在Serilog中,如何使用{Properties}格式说明符从JSON格式的日志消息中删除空括号?

时间:2019-07-05 11:23:57

标签: json asp.net-core serilog

我有以下outputTemplate字符串:

var formatString = "{NewLine}[{Timestamp:dd-MMM-yyyy HH:mm:ss}] {Level} {SourceContext}{NewLine}{Properties:j}{NewLine}{Message:lj}{NewLine}{Exception}";

我配置了多个浓缩器以添加和删除属性。在没有要记录的属性的情况下,我一直在一行上获取空的JSON括号。例如,当有要记录的属性时,我会收到类似以下的日志消息:

[05-Jul-2019 07:13:57] Information Microsoft.AspNetCore.Mvc
{ "UserName": "SomeUser" }
This is some log message with a property that was not removed by any of the enrichers.

但是,在没有属性的情况下,我得到这个

[05-Jul-2019 07:13:57] Information Microsoft.AspNetCore.Mvc
{}
This is some log message that contains no properties

空的JSON括号{}乱七八糟,只会增加噪音。如何扩展或覆盖Serilog以摆脱这些括号?

1 个答案:

答案 0 :(得分:0)

你可以使用这个 serilog 扩展:

dotnet add package Serilog.Expressions

github serilog-expressions: Conditional blocks:你可以做类似的事情 ... {#if Property is not null} ({Property}){#end} ...