I have following code.
Serilog.ILogger logger = Serilog.Log.Logger;
logger = new LoggerConfiguration()
.WriteTo.Sink(new FileSink(@"c:\temp\mylogs.txt", new JsonFormatter(), null)).MinimumLevel.Debug()
.CreateLogger();
var newType = new MyType() { Game = "Poker", HasValue = false, Name = "Dave", TimeOfEntry = DateTime.Now.AddDays(-1) };
logger.Debug("This is the new type {NewType} generated at {Time}", newType, DateTime.Now);
The log file shows following
{"Timestamp":"2015-02-11T00:53:51.8501574-05:00","Level":"Debug","MessageTemplate":"This is the new type {NewType} generated at {Time}","Properties":{"NewType":"ConsoleTestApp.MyType","Time":"2015-02-11T00:53:51.8491563-05:00"}}
The part that does not make sense in the log file output is this:
{"NewType":"ConsoleTestApp.MyType", ...
I was expecting something like this.
{"NewType": {Game = "Poker", HasValue = false, Name = "Dave", ...
Did I do something wrong?
By default, Serilog will ToString()
any unknown types passed into message templates.
To activate what Serilog calls "destructuring" (i.e. serialization) you need to prepend an at-sign @
to the property name:
logger.Debug("This is the new type {@NewType} generated at {Time}", newType, DateTime.Now);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With