Skip to content

Pass-through of M.E.L.ILogger.BeginScope() when using custom MEL ILoggingProvider as Serilog sink? #240

Open
@DavidHopkinsFbr

Description

@DavidHopkinsFbr

Hi,

I'm trying to wire up "legacy" custom implementations of Microsoft.Extensions.Logging.ILoggerProvider and Microsoft.Extensions.Logging.ILogger as a Serilog sink.

My Serilog configuration looks like this:

var customProviders = new LoggerProviderCollection();
customProviders.AddProvider(new CustomLoggerProvider());

// Configure Serilog
Log.Logger = new LoggerConfiguration()
      .MinimumLevel.Debug()
      .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
      .Enrich.WithThreadId()
      .Enrich.FromLogContext()
      // Hook up custom providers as Serilog sink
      .WriteTo.Providers(customProviders)
      // More log sinks here
      // [...]
      .CreateLogger();

IHostBuilder builder = Host.CreateDefaultBuilder(args)
      // Configure a stack of other app services
      // [...]
      // Replace default logging providers from Host.CreateDefaultBuilder() with Serilog
      .ConfigureLogging(logBuilder =>
      {
	      logBuilder.SetMinimumLevel(LogLevel.Trace);
	      logBuilder.ClearProviders();
      })
      .UseSerilog(providers: customProviders)

IHost host = builder.Build();

At runtime, my custom logger provider gets called and returns my custom ILogger implementations. So far so good.

This particular logging provider needs some context provided via ILogger.BeginScope() to complete its logging.

But, when I set breakpoints in my custom logger's implementations of ILogger.Log(...) and ILogger.BeginScope(...) methods, the breakpoint inside Log(...) gets hit, but the BeginScope<TState>(...) breakpoint never gets hit. The code being logged definitely calls _logger.BeginScope<TState>(foo) with the state information that the custom logger needs. It looks like Serilog isn't passing the BeginScope<TState>() information on to the CustomLogger instances, so they never get a chance to capture the information they need to complete the logging.

Am I using the APIs incorrectly or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions