8
votes

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:

  • SourceContext
  • RequestId
  • RequestPath

I presume they are being added by the ASP.Net Core logging framework. How can I get rid of them?

3

3 Answers

20
votes

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");
    }
}
2
votes

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.

1
votes

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.

  1. You can use attributes. Take a look at this post.
  2. Then there is by-ignoring. You need this Destructurama.ByIgnoring nuget.

Note you should not use both. Using both did not work for me.