we have the following serilog code, collections and simple strings are being written as expected, but when we try to log an object, serilog outputs the following rater than the object, is there any other setup that needs to be done to log all the properties of the metadata object ?
[16:40:59 INF] Processing {"$type": "Metadata"}
[16:40:59 INF] Processing Program+Metadata
Note: we are using this from a console application
using System;
using System.Collections.Generic;
using Serilog;
class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File("log.txt",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateLogger();
Metadata metadata =new Metadata();
metadata.Name = "hello";
var fruit = new[] { "Apple", "Pear", "Orange" };
Log.Information("In my bowl I have {Fruit}", fruit);
Log.Information("Processing {@HubTableMetadata}", metadata);
Log.Information("Processing {HubTableMetadata}", metadata);
Log.CloseAndFlush();
Console.Read();
}
public class Metadata
{
public string Name;
public string[] Tags;
public List<string> keys;
}
}
Serilog destructures properties, but your Metadata
class only defines fields.
To get the behaviour you want, update your Metadata
class to use properties:
public class Metadata
{
public string Name { get; set; }
public string[] Tags { get; set; }
public List<string> Keys { get; set; }
}
If you want to customize the output structured data, you can take a look the different Destructurama projects.
You might also be interested in reading this post "Using attributes to control destructuring in Serilog".