question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: MinimumLevel override ignores `SourceContext` of LogEvent

See original GitHub issue

The MinimumLevel override does not consider the SourceContext property defined in a LogEvent.

Repro:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .MinimumLevel.Override("MyContext", LogEventLevel.Information)
    .WriteTo.Console()
    .CreateLogger();

// Correct: DOES NOT write to sink, as expected
Log.ForContext(Serilog.Core.Constants.SourceContextPropertyName, "MyContext")
    .Verbose("If you see this, something is wrong (1)");

// Incorrect: DOES WRITE to sink
if (Log.Logger.BindMessageTemplate("If you see this, something is wrong (2)",
    propertyValues: null, out var parsedTemplate, out _))
{
    var properties = new[]
    {
        new LogEventProperty(Serilog.Core.Constants.SourceContextPropertyName,
            new ScalarValue("MyContext")),
    };

    var logEvent = new LogEvent(DateTimeOffset.Now, LogEventLevel.Verbose,
        exception: null, parsedTemplate, properties);

    // DOES WRITE to sink, but *NOT* expected
    Log.Write(logEvent);
}

image


Repro full project:

Tested:

  • Serilog v2.9.0
  • .NET Core 3.0 app

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
augustoproietecommented, Jun 12, 2020

I just saw this post on StackOverflow, and it seems the current behavior also affects sub-loggers:

var outputTemplate = "[{Level:u3}] {SourceContext}: {Message:lj}{NewLine}";

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.File("all.log", outputTemplate: outputTemplate)

    .WriteTo.Logger(lc => lc
        .MinimumLevel.Information()
        .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
        .WriteTo.File("some.log", outputTemplate: outputTemplate))
    .CreateLogger();

var msLogger = Log.Logger.ForContext(Constants.SourceContextPropertyName, "Microsoft.AspNet.Example");
var logger = Log.Logger.ForContext(Constants.SourceContextPropertyName, "MyClass");

msLogger.Verbose("example log; should not go in the minimal file");
msLogger.Information("example log; should not go in the minimal file");
msLogger.Warning("example log");
logger.Verbose("example log; should not go in the minimal file");
logger.Information("example log");

Log.CloseAndFlush();

In the example above, logs of type Information from the Microsoft source context are being written to some.log, when they shouldn’t be because of the .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning).

2reactions
nblumhardtcommented, Jun 17, 2020

Thanks for the follow-up, @augustoproiete .

Unfortunately, MinimumLevel.Override() is not supported at all by sub-loggers. We’ve updated the XDOC for MinimumLevel.Override() to call it out, but it seems we could still do better.

Implementing full support for it would also be great to explore, though I expect it’s going to be very challenging 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding MinimumLevel doesn't work in Serilog
You are overriding the MinimumLevel only for log messages sent from a SourceContext with the name LogTest , as per your configuration.
Read more >
Serilog 2.1 MinimumLevel.Override()
The first argument of Override is a source context prefix, which is normally matched against the namespace-qualified type name of the class ...
Read more >
Serilog Best Practices - Ben Foster
Log key events · Choose an appropriate logging level · Timing operations · Source Context · HTTP Logging · Avoid excessive production logging...
Read more >
Log with Serilog in .net 6.0
Initialize Serilog in appsettings.json file. "Serilog": { "MinimumLevel": { "Default": "Information", "Override": { "Microsoft" ...
Read more >
Common Log Options
Specifies if log output is created, and if so, the minimum level of log events generated. Log event levels, starting with the highest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found