如何从 ASP.Net Core 中的日志条目中删除属性
How to remove properties from log entries in ASP.Net Core
我在 ASP.Net Core 2.0 中使用 Serilog,使用 JsonFormatter 写入 RollingFile。
按照此处的说明进行配置:https://github.com/serilog/serilog-aspnetcore。
一切正常,但在每个日志条目中我都得到以下我没有记录的属性:
- SourceContext
- RequestId
- 请求路径
我假设它们是由 ASP.Net 核心日志框架添加的。我怎样才能摆脱它们?
是的,您可以摆脱它们。尝试使用日志模板:
_loggerConfiguration.WriteTo.Console(LogLevel.Debug, "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}");
在这种情况下,您不会在输出中看到提到的属性。
这可以通过在日志记录管道中插入一个增强器来实现:
.Enrich.With(new RemovePropertiesEnricher())
其中:
class RemovePropertiesEnricher : ILogEventEnricher
{
public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
{
le.RemovePropertyIfPresent("SourceContext");
le.RemovePropertyIfPresent("RequestId");
le.RemovePropertyIfPresent("RequestPath");
}
}
当你记录一个对象时,Serilog有destructuring的概念。
如果您想删除(忽略)这些对象中的某些属性以进行日志记录,有两种选择。
- 您可以使用属性。看看这个post。
- 然后是by-ignoring. You need this Destructurama.ByIgnoring nuget。
请注意,您不应同时使用两者。两者都对我不起作用。
我在 ASP.Net Core 2.0 中使用 Serilog,使用 JsonFormatter 写入 RollingFile。
按照此处的说明进行配置:https://github.com/serilog/serilog-aspnetcore。
一切正常,但在每个日志条目中我都得到以下我没有记录的属性:
- SourceContext
- RequestId
- 请求路径
我假设它们是由 ASP.Net 核心日志框架添加的。我怎样才能摆脱它们?
是的,您可以摆脱它们。尝试使用日志模板:
_loggerConfiguration.WriteTo.Console(LogLevel.Debug, "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}");
在这种情况下,您不会在输出中看到提到的属性。
这可以通过在日志记录管道中插入一个增强器来实现:
.Enrich.With(new RemovePropertiesEnricher())
其中:
class RemovePropertiesEnricher : ILogEventEnricher
{
public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
{
le.RemovePropertyIfPresent("SourceContext");
le.RemovePropertyIfPresent("RequestId");
le.RemovePropertyIfPresent("RequestPath");
}
}
当你记录一个对象时,Serilog有destructuring的概念。
如果您想删除(忽略)这些对象中的某些属性以进行日志记录,有两种选择。
- 您可以使用属性。看看这个post。
- 然后是by-ignoring. You need this Destructurama.ByIgnoring nuget。
请注意,您不应同时使用两者。两者都对我不起作用。