Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 934
LP13 Avatar asked Jun 18 '26 12:06

LP13


2 Answers

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).

like image 75
fededim Avatar answered Jun 20 '26 01:06

fededim


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"
      }
    }
  }
like image 41
Guru Stron Avatar answered Jun 20 '26 03:06

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!