【问题标题】:Override serilog minimumlevel for specific sink using appsettings.json使用 appsettings.json 覆盖特定接收器的 serilog minimumlevel
【发布时间】:2019-08-03 05:09:52
【问题描述】:

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

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

【问题讨论】:

  • 此类问题的第一站应该是 ASP.NET Core DocumentationFilter Rules,因为它详细记录了如何设置每个类/命名空间日志记录级别
  • @Tseng 这些示例用于内置日志记录。我正在使用 Serilog。
  • 只要你用ILoggerFactory插入它并在你的类中使用ILogger<T>,它也会被应用到它。见github.com/serilog/serilog-aspnetcoreILoggerFactoryILogger<T> 是抽象,日志配置适用于所有实现/基于这些抽象的配置

标签: asp.net-core serilog asp.net-core-2.1


【解决方案1】:

只需在特定接收器的配置部分中使用 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  */
        }
      }
    ],
    /* .. */
  }
}

【讨论】:

    【解决方案2】:

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

    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);
    }
    

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多