Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override serilog minimumlevel for specific sink using appsettings.json

There is documentation for "globally" overriding MinimumLevel in code, xml and json.

But can I override for a specific sink, using appsettings.json? For example, I want to log at warning level for class MyClass, but only for the console sink.

like image 676
lonix Avatar asked Aug 13 '18 07:08

lonix


2 Answers

Just use the restrictedToMinimumLevel param in the config section of the specific sink:

{
  /* .. */
  "Serilog": {
    "MinimumLevel": {
      "Default": "Verbose", /* <-- set the default level to the LOWEST applicable for your app  */
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console",
          "restrictedToMinimumLevel": "Information" /* <-- set the minimum level for specific sink  */
        }
      },
      {
        "Name": "File",
        "Args": {
          /* .. */
          "restrictedToMinimumLevel": "Warning" /* <-- set the minimum level for specific sink  */
        }
      },
      {
        "Name": "Udp",
        "Args": {
          /* .. */
          "restrictedToMinimumLevel": "Warning" /* <-- set the minimum level for specific sink  */
        }
      }
    ],
    /* .. */
  }
}
like image 174
vladimir Avatar answered Sep 23 '22 10:09

vladimir


I am interested in this question too. I can't find a solution. Apparently this cannot be done on correct.It can turn out like this:

  1. Create two sections of SERILOG (Serilog and Serilog2 or SerilogLow)
  2. configure the minimum logging level for each partition individually
  3. to register for both sections.

List item

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    var logger = new LoggerConfiguration().ReadFrom.ConfigurationSection(_configuration.GetSection("Serilog"))
    .ReadFrom.ConfigurationSection(_configuration.GetSection("SerilogLow")).CreateLogger();
    services.AddSingleton<ILogger>(logger);
}

HTH

like image 39
Alexander Brattsev Avatar answered Sep 23 '22 10:09

Alexander Brattsev