I'm using Serilog with ASP.Net Core 2.0, writing to RollingFile using JsonFormatter.
Followed the instructions here to configure: https://github.com/serilog/serilog-aspnetcore.
Everything works great, but in every log entry I get the following properties that I did not log:
I presume they are being added by the ASP.Net Core logging framework. How can I get rid of them?
This can be achieved by plugging an enricher into the logging pipeline:
.Enrich.With(new RemovePropertiesEnricher())
Where:
class RemovePropertiesEnricher : ILogEventEnricher
{
public void Enrich(LogEvent le, ILogEventPropertyFactory lepf)
{
le.RemovePropertyIfPresent("SourceContext");
le.RemovePropertyIfPresent("RequestId");
le.RemovePropertyIfPresent("RequestPath");
}
}
Yes, you can get rid of them. Try to use log template:
_loggerConfiguration.WriteTo.Console(LogLevel.Debug, "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}");
In this scenario, you won't see mentioned properties in your output.
When you are logging an object, Serilog has the concept of destructuring.
And if you want to remove(ignore) some properties in those objects for logging, there are two options.
Note you should not use both. Using both did not work for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With