14

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 Answers 3

30

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");
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@NicholasBlumhardt Not to hijack the post, but what and why are these injected automatically?
I had this very same question. Somewhere early in the request pipeline, a HostingLogScope 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<KeyValuePair<string,object>>, it maps naturally
This doesn't work with the "Exception" field.
Great answer. Is there a way to remove the sub-property (a property of property) as well?
Does it work for nested fields? I think, no
|
5

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.

Comments

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.