'Can a Serilog.ILogger be converted to a Microsoft.Extensions.Logging.ILogger?

I have code that logs to Microsoft.Extensions.Logging.ILogger (and extension methods, mostly).

I have configured Serilog to actually do the logging.

I can't find a way to convert a Serilog.ILogger to Microsoft.Extensions.Logging.ILogger. I assumed that Serilog would be implementing Microsoft.Extensions.Logging.ILogger, but it doesn't seem to.

Is there a way to get a Microsoft.Extensions.Logging.ILogger interface on the Serilog Logger?? --- Thanks

The code I have is:

        Serilog.Core.Logger seriLogger = new Serilog.LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger();
        Serilog.ILogger seriILogger = seriLogger;

        Microsoft.Extensions.Logging.ILogger msLogger = seriILogger;

produces

Cannot implicitly convert type 'Serilog.ILogger' to 'Microsoft.Extensions.Logging.ILogger'. An explicit conversion exists (are you missing a cast?)

a cast gets me a runtime error:

Unable to cast object of type 'Serilog.Core.Logger' to type 'Microsoft.Extensions.Logging.ILogger'



Solution 1:[1]

Apart from installing Serilog.AspNetCore you could also install Serilog.Extensions.Logging and use these lines of code. This will give you some ideas how UseSerilog() works under the hood (well, more or less this way :D).

var serilogLogger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .MinimumLevel.Verbose()
    .WriteTo.Debug() // Serilog.Sinks.Debug
    .CreateLogger();

var microsoftLogger = new SerilogLoggerFactory(serilogLogger)
    .CreateLogger<IMyService>(); // creates an instance of ILogger<IMyService>

Or if you need to use the logger in the DI container use these lines of code:

Log.Logger = serilogLogger;

var container = new ServiceCollection()
    .AddLogging(x => x.AddSerilog())
    .BuildServiceProvider(true);

Solution 2:[2]

TL;DR No, not directly. The compiler is correctly refusing to convert as the Serilog ILogger / Logger does not directly implement the MEL ILogger interface (The Serilog project started ~2012 and has always been designed to operate independent of any specific frameworks of that, or later times...)

There is however a bridging layer that implements the MEL ILogger interface, forwarding to the Serilog ILogger - There's an intro article on this topic which presents a good overview. Depending on your app type, you may not need all of that, but it should give you some context as to how it all fits together.

Solution 3:[3]

Maybe try this and register DI Serilog.ILogger to SerilogToMsLog

public class SerilogToMsLog: Serilog.ILogger
{
    private readonly ILogger<SerilogToMsLog> _logger;

    public SerilogToMsLog(ILogger<SerilogToMsLog> logger)
    {
        _logger = logger;
    }

    public void Write(LogEvent logEvent)
    {
        if (logEvent.Level == LogEventLevel.Error)
        {
            _logger.LogError(logEvent.Exception,logEvent.RenderMessage());
        }
        
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Van Vu