无法将Serilog配置代码行转换为json配置

时间:2017-11-30 02:36:48

标签: c# logging asp.net-core serilog

我找到了一篇帖子,其中博主解释了如何通过LogEvent级别过滤到Serilog配置的单独文件。我正在我的appsettings.json中完成所有的Serilog配置。如何看待json配置,我似乎无法想象如何json lambda表达式....

Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning).WriteTo.RollingFile(@"Logs\Warning-{Date}.log"))

我正在使用我的appsettings.json中的Serilog配置,并尝试转换此

.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning).WriteTo.RollingFile(@"Logs\ApplicationName\Serilog\Warning-{Date}.log"))

到json,包含在我的appsettings文件的Serilog部分

修改 appsettings部分显示在这里

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}"
    }
  },
  {
    "Name": "Seq",
    "Args": { "serverUrl": "http://localhost:5341" }
  },
  {
    "Name": "Async",
    "Args": {
      "configure": [
        {
          "Name": "RollingFile",
          "Args": { "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/log-{Date}.log" }
        }
      ]
    }
  }
],
"SubLogger": {
  "Level": "Warnings",
  "pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warnings/log-{Date}.log"
},

子记录器pathFormat不会生成与RollingFile pathFormat相同的文件夹命名

File Explorer

1 个答案:

答案 0 :(得分:1)

目前,Serilog不支持通过JSON appsettings配置子记录器。请参阅github上的issue

实际上,这不是一件容易的事,因为您将Func<LogEvent, bool>传递给ByIncludingOnly()过滤器。将配置数据从json文件映射到c#代码并非易事。

但是,如果您只想为特定日志级别创建子记录器,则可以将JSON配置中的配置与ByIncludingOnly()过滤器结合使用。

定义将保留过滤器配置的POCO:

public class SubLoggerConfiguration
{
    public LogEventLevel Level { get; set; }

    private string pathFormat;
    public string PathFormat
    {
        get => pathFormat;
        set => pathFormat = value.Replace("%APPLICATION_NAME%", Environment.GetEnvironmentVariable("APPLICATION_NAME"));
    }
}

SubLogger部分添加到您的JSON配置:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.RollingFile"
    ],
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": { "pathFormat": "c:\\Logs\\log-{Date}.log" }
      }
    ],

    "SubLogger": {
      "Level": "Warning",
      "pathFormat": "Logs\\ApplicationName\\Serilog\\Warning-{Date}.log"
    } 
  }
}

将它保留在原生的Serilog部分中是一个好主意,它不会破坏Serilog本身的配置。

然后从配置文件加载SubLogger配置:

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("AppSettings.json");
IConfiguration configuration = configurationBuilder.Build();
SubLoggerConfiguration subLoggerConfiguration = new SubLoggerConfiguration();
configuration.GetSection("Serilog:SubLogger").Bind(subLoggerConfiguration);

请注意,您必须安装Microsoft.Extensions.Configuration.Binder NuGet包才能将绑定配置发送到POCO。

现在,subLoggerConfiguration将包含日志所需的日志级别和路径格式。您可以使用此设置来调用ByIncludingOnly()过滤器:

Logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == subLoggerConfiguration.Level).WriteTo.RollingFile(subLoggerConfiguration.PathFormat));