[英]Binding an appsettings.json section to a list of POCO
Given these two classes 鉴于这两类
public class SerilogSubLoggerConfigurations
{
public List<SerilogSubLoggerConfiguration> SerilogConfigurations { get; set; }
}
public class SerilogSubLoggerConfiguration
{
private string _pathFormat;
public LogEventLevel Level { get; set; }
public string PathFormat
{
get => _pathFormat;
set => _pathFormat = value.Replace("%APPLICATION_NAME%", Environment.GetEnvironmentVariable("APPLICATION_NAME"));
}
}
and an apssettings.json section that looks like this 和apssettings.json部分看起来像这样
"Serilog": {
"SubLogger": [
{
"Level": "Information",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Information/log-{Date}.log"
},
{
"Level": "Warning",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warning/log-{Date}.log"
},
{
"Level": "Critical",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}
],
}
I want to bind the SubLogger
section to the SerilogSubLoggerConfigurations
class. 我想将SubLogger
部分绑定到SerilogSubLoggerConfigurations
类。 I cannot seem to figure out how to get this configuration to create that class with the three items (of type SerilogSubLoggerConfiguration
). 我似乎无法弄清楚如何获得此配置以使用三个项目(类型为SerilogSubLoggerConfiguration
)创建该类。
In Json config, array of your sublogger objects is called SubLogger
but in configuration POCO SerilogSubLoggerConfigurations
it's called SerilogConfigurations
. 在Json config中,子记录器对象的数组称为SubLogger
但在配置POCO SerilogSubLoggerConfigurations
它称为SerilogConfigurations
。 That's why configuration binder could not match them. 这就是配置绑定器无法匹配它们的原因。
In ASP.NET Core there is no possibility (at least for this moment) to change the name of mapped properties, they should match the names of configuration sections. 在ASP.NET Core中,(至少目前)无法更改映射属性的名称,它们应与配置节的名称匹配。 Check this answer for details. 查看此答案以获取详细信息。
So you should just rename either POCO property or JSON section name. 因此,您应该只重命名POCO属性或JSON节名称。 In your case SubLoggers
seems to be the most appropriate name: 在您的情况下, SubLoggers
似乎是最合适的名称:
public class SerilogSubLoggerConfigurations
{
public List<SerilogSubLoggerConfiguration> SubLoggers { get; set; }
}
{
"Serilog": {
"SubLoggers": [
{
"Level": "Information",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Information/log-{Date}.log"
},
{
"Level": "Warning",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Warning/log-{Date}.log"
},
{
"Level": "Critical",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}
]
}
}
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("AppSettings.json");
IConfiguration configuration = configurationBuilder.Build();
SerilogSubLoggerConfigurations subLoggerConfigurations = configuration.GetSection("Serilog").Get<SerilogSubLoggerConfigurations>();
One more note. 还有一张便条。 Serilog.Events.LogEventLevel
enum does not have a Critical
level, it has Error
and Fatal
. Serilog.Events.LogEventLevel
枚举没有Critical
级别,它具有Error
和Fatal
。 That's why this item 这就是为什么这个项目
{
"Level": "Critical",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}
will not be loaded correctly. 将无法正确加载。 You should change it to: 您应该将其更改为:
{
"Level": "Error",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Error/log-{Date}.log"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.