Asp.net-Core

如何從 ASP.Net Core 中的日誌條目中刪除屬性

  • March 11, 2022

我正在使用帶有 ASP.Net Core 2.0 的 Serilog,使用 JsonFormatter 寫入 RollingFile。
按照此處的說明進行配置:https ://github.com/serilog/serilog-aspnetcore 。
一切都很好,但是在每個日誌條目中,我都會得到以下未記錄的屬性:

  • 源上下文
  • 請求編號
  • 請求路徑

我認為它們是由 ASP.Net Core 日誌框架添加的。我怎樣才能擺脫它們?

這可以通過將濃縮器插入日誌管道來實現:

.Enrich.With(new RemovePropertiesEnricher())

在哪裡:

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

是的,你可以擺脫它們。嘗試使用日誌模板:

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

在這種情況下,您不會在輸出中看到提到的屬性。

引用自:https://stackoverflow.com/questions/47176191