首页 > 其他 > 详细

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

时间:2020-03-31 21:50:24      阅读:88      评论:0      收藏:0      [点我收藏+]

问题

使用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的格式化输出冲突,会被当作格式化字符

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

原文:https://www.cnblogs.com/mosakashaka/p/12608747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!