Serilog格式化打印,缺少大括号

问题

使用seilog打印输出,发现特定内容缺少了结尾的大括号:

字符串原始内容:

"{\"cont\":{\"content\":\"{\\\"Test Datetime\\\":\\\"20180426173039\\\"}\",\"creationTime\":\"2018-04-26 17:34:09\"}}"

正确输出

{"cont":{"content":"{\"Test Datetime\":\"20180426173039\"}","creationTime":"2018-04-26 17:34:09"}}

实际输出

{"cont":{"content":"{\"Test Datetime\":\"20180426173039\"}","creationTime":"2018-04-26 17:34:09"}

差异仅仅在最后缺少了这个大括号。

问题解决

原本打算提出问题了,但是在git issue中查找到了这个问题:Writing JSON string drops bracket in serilog output。与该问题对应的解释在这篇文章 C# 6 string interpolation and Serilog

原因是,不应在serilog中使用c# 6.0的interpolation特性。

错误写法:

log.Information($"output string: [{str}]");

正确写法:

log.Information("output string: [{str}]", str);

使用c#的interploation拼接字符串,和serilog的格式化输出冲突,会被当作格式化字符

猜你喜欢

转载自www.cnblogs.com/mosakashaka/p/12608747.html