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'
But to my surprise, Serilog is using it's own ILogger interface - bummer!
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With