Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 405
Chris Avatar asked Dec 24 '20 22:12

Chris


People also ask

Does Serilog use ILogger?

But to my surprise, Serilog is using it's own ILogger interface - bummer!

What is Microsoft ILogger?

A logging provider displays or stores logs to a particular medium such as a console, a debugging event, an event log, a trace listener, and others. Microsoft provides various logging providers as NuGet packages. The following table lists important logging providers.

What is the use of ILogger in .NET core?

ILoggerFactory is a factory interface that we can use to create instances of the ILogger type and register logging providers. It acts as a wrapper for all the logger providers registered to it and a logger it creates can write to all the logger providers at once.

What is .NET ILogger?

Formats the message and creates a scope. Log(ILogger, LogLevel, EventId, Exception, String, Object[]) Formats and writes a log message at the specified log level. Log(ILogger, LogLevel, EventId, String, Object[]) Formats and writes a log message at the specified log level.


2 Answers

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);
like image 135
mihails.kuzmins Avatar answered Dec 02 '22 23:12

mihails.kuzmins


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.

like image 32
Ruben Bartelink Avatar answered Dec 03 '22 01:12

Ruben Bartelink