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

问题描述 投票:4回答:3

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

  • SourceContext
  • 的requestId
  • RequestPath

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

asp.net-core serilog
3个回答
6
投票

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

.Enrich.With(new RemovePropertiesEnricher())

哪里:

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

1
投票

是的,你可以摆脱它们。尝试使用日志模板:

_loggerConfiguration.WriteTo.Console(LogLevel.Debug, "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}");

在这种情况下,您将不会在输出中看到提到的属性。


0
投票

当您记录对象时,Serilog具有destructuring的概念。

如果要删除(忽略)这些对象中的某些属性以进行日志记录,则有两个选项。

  1. 您可以使用属性。看看这个post
  2. 然后有by-ignoring。你需要这个Destructurama.ByIgnoring nuget

请注意,您不应同时使用两者。使用它们对我来说都不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.