无法将Serilog配置代码行转换为json配置 [英] Trouble converting Serilog Configuration code line to json configuration

查看:63
本文介绍了无法将Serilog配置代码行转换为json配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一篇博文,博主解释了如何按LogEvent级别过滤到单独的文件以进行Serilog配置.我正在我的appsettings.json中完成所有Seri​​log配置.

I found a post where the blogger explained how to filter by LogEvent level to a separate file for Serilog configuration. I am doing all my Serilog configuration in my appsettings.json. How would this look in json configuration, I can't seem to figure how to json the lambda expression....

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的配置,并试图将其转换

I am using configuration for Serilog from my appsettings.json and am trying to convert this

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

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

to json, to include in my Serilog section of my appsettings file

appsettings部分显示在这里

appsettings partial shown here

 "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相同的文件夹命名

The sub logger pathFormat is not producing the same folder naming as the RollingFile pathFormat

推荐答案

目前,Serilog不支持通过JSON应用设置配置子记录器.在github上查看此问题.

For this moment Serilog does not support configuration of sub-loggers through JSON appsettings. See this issue on github.

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

It's not an easy task actually because you pass Func<LogEvent, bool> to ByIncludingOnly() filter. Mapping configuration data from json file to c# code is not a trivial task.

但是,如果您仅对创建特定日志级别的子记录器感兴趣,则可以将JSON配置中的配置与 ByInclusionOnly()过滤器结合使用.

However if you are just interested in creation of sub-logger for specific log level, you could combine configuration from JSON config with ByIncludingOnly() filter.

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

Define a POCO that will hold filter configuration:

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

在您的JSON配置中添加 SubLogger 部分:

Add SubLogger section to your JSON config:

{
  "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本身的配置.

It's a good idea to keep it inside native Serilog section, it will not break configuration of Serilog itself.

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

Then load SubLogger configuration from config file:

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.

Note that you have to install Microsoft.Extensions.Configuration.Binder NuGet package for binding configuration to a POCO.

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

Now subLoggerConfiguration will contain desired log level and path format for the log. You can use this settings to call ByIncludingOnly() filter:

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

这篇关于无法将Serilog配置代码行转换为json配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆