Serilog 没有产生预期的 json

Serilog does not produce expected json

我有以下代码。

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);

日志文件显示如下

{"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"}}

日志文件输出中没有意义的部分是这样的:

{"NewType":"ConsoleTestApp.MyType", ...

我期待这样的事情。

{"NewType": {Game = "Poker", HasValue = false, Name = "Dave", ...

我是不是做错了什么?

默认情况下,Serilog 会将 ToString() 任何未知类型传递到消息模板中。

要激活 Serilog 调用的 "destructuring"(即序列化),您需要在 属性 名称前添加一个 at 符号 @

logger.Debug("This is the new type {@NewType} generated at {Time}", newType, DateTime.Now);