使用 appsettings.json 覆盖特定接收器的 serilog 最低级别

lon*_*nix 10 serilog asp.net-core asp.net-core-2.1

在代码、 xml 和json中有“全局”覆盖 MaximumLevel 的文档。

但是我可以使用 覆盖特定接收器appsettings.json吗?例如,我想以警告级别记录 class MyClass,但仅限于控制台接收器。

vla*_*mir 10

只需在特定接收器的配置部分中使用restrictedToMinimumLevel参数即可:

{
  /* .. */
  "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  */
        }
      }
    ],
    /* .. */
  }
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*sev 1

我对这个问题也很感兴趣。我找不到解决办法。显然这不能正确完成。结果可能是这样的:

  1. 创建 SERILOG 的两个部分(Serilog 和 Serilog2 或 SerilogLow)
  2. 单独配置每个分区的最低日志记录级别
  3. 注册这两个部分。

项目清单

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);
}
Run Code Online (Sandbox Code Playgroud)

华泰