【问题标题】:Is there a way to configure multiple Serilog RollingFiles through appSetting configuration有没有办法通过appSetting配置来配置多个Serilog RollingFiles
【发布时间】:2016-02-10 12:40:27
【问题描述】:

有没有办法通过appSetting配置多个Serilog RollingFiles?

我想为信息和错误级别创建单独的日志文件。

【问题讨论】:

    标签: serilog


    【解决方案1】:

    在 appsettings 中配置多个日志非常简单,任务是使用过滤器。我花了将近 3 个小时试图弄清楚如何存档。

    我最终做了以下事情:

    Startup.cs / Global.asax.cs

    Log.Logger = new LoggerConfiguration()
               .WriteTo
               .Logger(x => x.Filter
               .ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Error)
               .ReadFrom
               .AppSettings("error"))
    
               .WriteTo
               .Logger(x => x.Filter
               .ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Information)
               .ReadFrom
               .AppSettings("info")).CreateLogger()
    

    Web.Config

    <add key ="error:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
    <add key ="error:serilog:write-to:RollingFile.pathFormat" value="C:\log\error {Date}.txt"/>
    <add key ="error:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>
    
    <add key ="info:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
    <add key ="info:serilog:write-to:RollingFile.pathFormat" value="C:\log\info {Date}.txt"/>
    <add key ="info:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>
    

    【讨论】:

      【解决方案2】:

      不直接 - 可以使用设置前缀,例如:

      .ReadFrom.AppSettings()
      .ReadFrom.AppSettings(settingPrefix: "2")
      

      然后添加额外的接收器,如:

      <add key="2:serilog:write-to:RollingFile.pathFormat" value="..." />
      

      将其正确地放入应用设置配置提供程序中已经有一段时间了。

      如果可以在代码中配置接收器,那可能就是要走的路。

      【讨论】:

      • 感谢您的回答。我试过了,但它没有用,虽然你指出了我正确的方向。我不得不查看代码来弄清楚为什么它没有选择配置。代码是:` _settingPrefix = settingPrefix == null ? "serilog:" : string.Format("{0}:serilog:", settingPrefix); ` 所以我必须提供如下前缀:
      • 在上面的注释中,“custom”是 API 中使用的前缀,即。 '.ReadFrom.AppSettings(settingPrefix: "custom")`
      • 谢谢 - 修正了这个例子。
      • 嗨@NicholasBlumhardt - 根据您上面的评论,AppSettings 包是否支持此功能?谢谢
      • 嘿@RobDavis - 不,虽然 JSON 配置语法不会遇到这个问题。
      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      相关资源
      最近更新 更多