serilog / serilog-extensions-logging

Serilog provider for Microsoft.Extensions.Logging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MinimumLevel.Override is not Working

avireddy02 opened this issue · comments

hey there
I'm trying to enable debug logging for a specific class using serilog and found few examples using MinimumLevel.Override. Unfortunately its not working as intended.

Ex: Sample GitRepo
In this example I have configured "Serilog.Ext.Logging.Bug._174.Controllers.WeatherForecastController" to Verbose, LogTrace is not showing up in console

var seriConfig = new LoggerConfiguration()
                       .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                       .MinimumLevel.Override("System", LogEventLevel.Error)
                       .MinimumLevel.Override("Serilog.Ext.Logging.Bug._174.Controllers.WeatherForecastController", LogEventLevel.Verbose)
                       .Enrich.FromLogContext() 
                      ;

            seriConfig.WriteTo.Console(
                    restrictedToMinimumLevel: LevelConvert.ToSerilogLevel(LogLevel.Information),
                    standardErrorFromLevel: LogEventLevel.Error);
            Log.Logger = seriConfig.CreateLogger();
namespace Serilog.Ext.Logging.Bug._174.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]  { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"};
        private readonly ILogger<WeatherForecastController> _logger;
        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            _logger.LogTrace("{@test}", new   { A="Test"});
            _logger.LogTrace("Test1");
            _logger.LogInformation("Info 1");
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

Console Out:
[18:28:30 INF] Info 1

Wouldn't the restrictedToMinimumLevel on the WriteTo.Console call always mean that only things at Info or above get logged, regardless of the level overrides?

I was reading docs for Override and found below which means working as designed. I'm looking for something like AspNetCore Logging which supports writing debug for a specified Namespace/Full TypeName despite default LogLevel is as Info. This will help us identify issue much faster with fewer debug Logs.

Ex: While investigating a Routing Issue, adding Override for "Microsoft.AspNetCore.Routing.EndpointMiddleware" to Debug is lot helpful than setting LogLevel to Debug at app level

Docs:
"Logger vs. sink minimums - it is important to realize that the logging level can only be raised for sinks, not lowered. So, if the logger's MinimumLevel is set to Information then a sink with Debug as its specified level will still only see Information level events. This is because the logger-level configuration controls which logging statements will result in the creation of events, while the sink-level configuration only filters these. To create a single logger with a more verbose level, use a separate LoggerConfiguration"

I don't quite get the idea of using separate LoggerConfigurations. Ability to lower minimum log level by source context would be still useful.

This issue is over a year old, seemingly without a conclusion. I second the opinion of @FLAMESpl that the ability to lower minimum log level by source context would be useful. This was the behavior I expected from reading the documentation I have found - I didn't find the section quoted by @avireddy02.

Any words from contributors on this issue?

Hi! Some background:

  • MinimumLevel (and overrides) control which log statements generate LogEvents
  • Sink-specific restrictions are filters put in front of the sink that choose which LogEvents get through

In the sample code, minimum level overrides mean that WeatherForecastController's Verbose log statements result in log events, but, the filter in front of the sink subsequently discards them.

There's no reason in the sample code to have restrictedToMinimumLevel on the Console sink; just removing it will "fix" the issue.

If you're in a different situation and need to use more nuanced minimum level overrides + sink-specific filtering, then WriteTo.Conditional() is a better alternative to restrictedToMinimumLevel.

Serilog is designed as an event processing pipeline, so this kind of behavior makes a bit more sense from that angle.

Hope this helps! Closing as by-design, but if the explanation above isn't clear, please let me know below and I'll add more info :-)