LP13
LP13

Reputation: 34109

Serilog ignoring LogLevel from Microsoft.Extension.Logging

I am using NET5 ASP.NET MVC application. Application is using Serilog.AspNetCore 3.4.0 for logging

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Error"
    }
  },
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console"],
    "WriteTo": [
      {
        "Name": "Console"
      }
    ]
  }
}

Program.cs

     public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                   .UseStartup<Startup>()
                   .ConfigureLogging((hostingContext, logging) =>
                   {
                       logging.ClearProviders();                    
                   })                       
                   .UseSerilog((hostingContext, logging) =>
                   {
                      logging.ReadFrom.Configuration(hostingContext.Configuration);
                   });
            });

I have also tried

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                   .UseStartup<Startup>()
                   .ConfigureLogging((hostingContext, logging) =>
                   {
                       logging.ClearProviders();
                       Log.Logger = new LoggerConfiguration()
                       .ReadFrom.Configuration(hostingContext.Configuration)                           
                       .CreateLogger();
                       logging.AddSerilog();
                   });                       
               
            });

Issue
My expectation is no Information log will be shown in Console since default LogLevel is Error. However, that is not working. In console I see every request is getting logged including Information

Throughout my application I am using Microsoft.Extensions.Logging.ILogger<MyClassName> to log information. All those statements are actually logging Info even if the LogLevel is Error.

Looks like Serilog ignoring LogLevel from Microsoft.Extensions.Logging.

Note that, I can set serilog's restrictedToMinimumLevel property to Error and that stops logging information. However I think serilog should obey the LogLevel from Microsoft.Extension.Logging

Upvotes: 8

Views: 9332

Answers (3)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31797

If you use Serilog.Extensions.Logging and AddSerilog() on ILoggingBuilder, you'll get what you're expecting.

However, IHostBuilder.UseSerilog() (provided by Serilog.Extensions.Hosting, via Serilog.AspNetCore) is almost always a better choice, hence that's what all the Serilog docs show.

Although it seems more "integrated" to use the default configuration section this way, what you're getting behind the scenes is actually two different logging frameworks running at the same time, e.g. in the AddSerilog() case (or in the default NLog configuration):

ILogger<T> -->
    MEL (routing, configuration, levels, filters) -->
        Serilog (routing, configuration, levels, filters)

Logging is supposed to be very predictable/reliable and lightweight. Having two logging frameworks running at the same time erodes this and creates problems where the levels and filters specified for one framework don't match the other. (E.g. Debug and trace logs not printed when using Nlog and ILoggerFactory in .NetCore console app).

In the UseSerilog() case, (and when NLog's ReplaceLoggerFactory option is specified - mentioned in a comment on the SO thread above), you get:

ILogger<T> -->
    Serilog (routing, configuration, levels, filters)

You give up support for the default "Logging" configuration section, but in exchange, you get rid of a whole logging framework at runtime, and all of your Serilog (or NLog) configuration just works, with the Microsoft.Extensions.Logging ILogger<T> available to your application code and the framework.

Upvotes: 1

fededim
fededim

Reputation: 219

If you check the project's Github page it advises to actually remove the standard "Logging" section in appsettings.json :-(

Serilog's guidelines

I think this is bad since it actually breaks the compatibility with Microsoft.Extensions.Logging framework (you can't anymore change the actual log provider (Log4Net, NLog, Serilog) without changing the appsettings.json).

Upvotes: 7

Guru Stron
Guru Stron

Reputation: 142008

Use MinimumLevel property:

  "Logging": {
    "MinimumLevel": {
      "Default": "Error"
    }
  }

Also it supports overrides for categories:

  "Logging": {
    "MinimumLevel": {
      "Default": "Error",
      "Override": {
        "System": "Information",
        "Microsoft": "Information",
        "Microsoft.Hosting.Lifetime": "Information",
        "Microsoft.EntityFrameworkCore": "Debug"
      }
    }
  }

Upvotes: 4

Related Questions