serilog.settings.appsettings 和文件翻转间隔的问题

Problems with serilog.settings.appsettings and file rollover interval

我正在使用 SeriLog 登录我的基于 IdentityServer3 的身份验证服务。我只是将 serilog.sinks.literate 插件替换为 serilog.sinks.file 插件,以便获得 File 插件提供的滚动文件功能。当我使用 File 插件时,在 Startup.cs,它按我预期的那样工作。 (我只使用 RolloverInterval.Minute 进行测试。我将在部署代码时切换到 RolloverInterval.Day。)

Log.Logger = new LoggerConfiguration()
.WriteTo.File("E:\Site\AuthSvc\Trace.log", rollingInterval: RollingInterval.Minute, retainedFileCountLimit: 10)
.CreateLogger();

如我所说,这按预期工作,每分钟创建一个新文件。

现在我正在尝试使用 serilog.settings.appsettings 插件来管理 appSettings 中的所有配置,而不是在代码中。所以我把上面的代码改成了:

Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();

并且我在 web.config appSettings 中添加了以下内容:

<add key="serilog:minimum-level" value="Debug"/>
<add key="serilog:using:File" value="Serilog.Sinks.File"/>
<add key="serilog:write-to:File.path" value="E:\Site\AuthSvc\Trace.log"/>
<add key="serilog:write-to:File.rollingInterval" value="RollingInterval.Minute"/>
<add key="serilog:write-to:File.retainedFileCountLimit" value="10"/>

当我 运行 使用上述配置的服务时,我收到错误消息 "Requested value 'RollingInterval.Minute' was not found." 任何 RollingInterval 值都会发生类似的错误。

如果我删除最后两个 appSettings,它可以正常工作。是我做错了什么,还是 appSettings 插件不支持 File sink 插件的滚动文件功能?

我不确定这是否记录在案(我会检查),但是当通过 Serilog.Settings.AppSettings 设置枚举值时,您只指定枚举成员名称,而不指定枚举名称。

例如而不是

<add key="serilog:write-to:File.rollingInterval" value="RollingInterval.Minute"/>

使用

<add key="serilog:write-to:File.rollingInterval" value="Minute"/>

这在 appsettings.json 文件中对我有用:

"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "C:\Temp\log-.log",
          "rollingInterval": "Day"
        }
      }
    ],
    "Properties": {
      "Application": "MyService"
    }
  }