如何从ASP.Net Core中的日志条目中删除属性

And*_*rew 4 serilog asp.net-core

我正在使用Serilog和ASP.Net Core 2.0,使用JsonFormatter写入RollingFile.按照此处的说明进行配置:https://github.com/serilog/serilog-aspnetcore.一切都很好,但在每个日志条目中我得到以下没有记录的属性:

  • SourceContext
  • 的requestId
  • RequestPath

我认为它们是由ASP.Net Core日志框架添加的.我怎么能摆脱他们?

Nic*_*rdt 7

这可以通过将richper插入日志记录管道来实现:

.Enrich.With(new RemovePropertiesEnricher())
Run Code Online (Sandbox Code Playgroud)

哪里:

class RemovePropertiesEnricher : ILogEventEnricher
{
    public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
    {
        le.RemovePropertyIfPresent("SourceContext");
        le.RemovePropertyIfPresent("RequestId");
        le.RemovePropertyIfPresent("RequestPath");
    }
}
Run Code Online (Sandbox Code Playgroud)