Search code examples
asp.net-coreserilog

ILogger in Asp.net Core and Serilog


I have a question,

Is there any concern if I use ILogger in Serilog on behalf of Microsoft logger?

public void ConfigureServices(IServiceCollection services)
{
     services.AddSingleton(Log.Logger);
}

Then use from ILogger in Serilog namespace.

 _logger.Error(exception, "Error", exception.Message, exceptionId);

What is your idea?


Solution

  • Microsoft.Extensions.ILogger is an abstraction that decouples your application code (the code that writes log entries) from the underlying logging framework.

    Using this abstraction brings the benefit, that you could easily change the Logging Framework underneath (e.g. replace Serilog with NLog) and don't have to update all references in your application code.

    Also using Microsoft's abstractions also allows you to configure logging of your application code and logging of Microsoft SDKs you might use at a single place.

    The downside of abstractions is that you have to aggree to a common minimum interface provided by all logging frameworks. It's not so easy to use Framework-Specific features this way.

    So most of the time i would advise using the abstraction.

    If you have very specific features from Serilog you would like to interact with you could think about using ILogger from Serilog directly. However you can configure serilog in the provider registration as well to a high degree and probably get the best of both worlds.