Bug: MinimumLevel override ignores `SourceContext` of LogEvent
See original GitHub issueThe 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);
}
Repro full project:
Tested:
- Serilog v2.9.0
- .NET Core 3.0 app
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just saw this post on StackOverflow, and it seems the current behavior also affects sub-loggers:
In the example above, logs of type
Information
from theMicrosoft
source context are being written tosome.log
, when they shouldn’t be because of the.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
.Thanks for the follow-up, @augustoproiete .
Unfortunately,
MinimumLevel.Override()
is not supported at all by sub-loggers. We’ve updated the XDOC forMinimumLevel.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 😃