1

I'm planning to use Serilog in my ASP.NET Core 3 application and I would like to use inline initialization. I'm only going to use the ILogger<T> interface from my application.

Is there any particular reason for assigning a new logger instance to the Log.Logger property if I don't intend to directly use the logging methods inside the Log class?

I found the following sample on the Serilog integration for ASP.NET Core page:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateLogger();

I understand that for logging application start-up errors I need to create a new logger instance, but couldn't I just use it like:

using Serilog;

public class Program
{
    public static int Main(string[] args)
    {
        using var startupLogger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        try
        {
            startupLogger.Information("Starting web host");
            CreateHostBuilder(args).Build().Run();
            return 0;
        }
        catch (Exception ex)
        {
            startupLogger.Fatal(ex, "Host terminated unexpectedly");
            return 1;
        }
    }
}
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
Botond Botos
  • 1,202
  • 13
  • 20
  • 1
    Note that if any library you're consuming uses [LibLog](https://github.com/damianh/LibLog), it will want the static logger to be configured. Not too common now that MS released their own logging framework, but still possible some older libraries may use it, such as [Hangfire](https://github.com/HangfireIO/Hangfire/blob/a07ad0b9926923db75747d92796c5a9db39c1a87/src/Hangfire.Core/App_Packages/LibLog.1.4/LibLog.cs). – mason May 29 '20 at 18:59

1 Answers1

0

By setting

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .CreateLogger();

And

    public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseSerilog(); // <-- Add this line;
                }

You setup your logger "globally" and it is accessible in the whole application without need to be injected.

In any other class in the project or projects referencing it, you can log using by:

Log.Error("");

using the logger settings you set in the start up.

Answering your question if it is possible to inject Serilog logger, the answer is negative. You cannot access Serilog through DI in Net Core. It's a static. You could probably hack it around if you want it, that I would not recommend. You could find some examples here.

Stelios Giakoumidis
  • 2,153
  • 1
  • 7
  • 19
  • 1
    Thank you for your answer. I don't intend to use the static methods on the `Log` class, I'm only going to use injected `ILogger` instances. My question is whether it is required or could it have any side effects if I don't initialize the `Log.Logger` property. – Botond Botos May 29 '20 at 13:53