无法将 Serilog 配置代码行转换为 json 配置
Trouble converting Serilog Configuration code line to json configuration
我找到了一个 post 博主解释了如何通过 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,包括在我的应用程序设置文件的 Serilog 部分
编辑:
此处显示部分应用程序设置
"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"
},
子记录器路径格式未生成与 RollingFile 路径格式相同的文件夹命名
目前 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));
我找到了一个 post 博主解释了如何通过 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,包括在我的应用程序设置文件的 Serilog 部分
编辑: 此处显示部分应用程序设置
"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"
},
子记录器路径格式未生成与 RollingFile 路径格式相同的文件夹命名
目前 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));