12

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?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Andrew
  • 1,139
  • 1
  • 12
  • 27

3 Answers3

27

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");
    }
}
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • 1
    @NicholasBlumhardt Not to hijack the post, but what and why are these injected automatically? – Justin Mar 13 '18 at 16:01
  • I had this very same question. Somewhere early in the request pipeline, a [HostingLogScope](https://github.com/aspnet/Hosting/blob/5543d29af1e6b4f2f5dfa8822db12b666f33d1b5/src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs) is created. As you can see from the source, this log scope is responsible for adding (most) of the offending properties. I'm missing a link for where it *exactly* happens, but Serilog then takes this context object and turns it into the properties of a log entry. Since the context is a IReadOnlyList>, it maps naturally – Saint Domino Feb 08 '19 at 08:23
  • This doesn't work with the "Exception" field. – Junaid Sep 02 '21 at 11:35
  • Great answer. Is there a way to remove the sub-property (a property of property) as well? – Junaid Sep 16 '21 at 19:01
  • I'm wondering why the [JsonIgnore] attribute is not applied for this? – Michael Nov 02 '21 at 10:06
  • Does it work for nested fields? I think, no – RouR Nov 25 '22 at 18:08
2

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.

Leonid
  • 1,071
  • 2
  • 15
  • 27
  • Thanks Leonid, my logging to console works fine. Because like in your example I have a message template there that does not contain these parameters. How to I get rid of them when logging to file: .WriteTo.RollingFile(new JsonFormatter(), logFilePath, LogEventLevel.Information)? I do not want to provide a template here, just want to log raw json. – Andrew Nov 08 '17 at 11:18
  • 1
    Currently, this feature is not supported (I checked the source code). I see a workaround, you can use kind of template that forms JSON file. Or you can implement this feature and create a pull request to their repository. – Leonid Nov 08 '17 at 12:12
2

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.

VivekDev
  • 20,868
  • 27
  • 132
  • 202