将 appsettings.json 部分绑定到 POCO 列表
Binding an appsettings.json section to a list of POCO
鉴于这两个 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"));
}
}
和一个看起来像这样的 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"
}
],
}
我想将 SubLogger
部分绑定到 SerilogSubLoggerConfigurations
class。我似乎无法弄清楚如何获得此配置以使用三个项目(SerilogSubLoggerConfiguration
类型)创建 class。
在 Json 配置中,您的子记录器对象数组称为 SubLogger
,但在配置 POCO SerilogSubLoggerConfigurations
中称为 SerilogConfigurations
。
这就是配置活页夹无法匹配它们的原因。
在 ASP.NET Core 中,不可能(至少目前)更改映射属性的名称,它们应该与配置部分的名称相匹配。查看此 answer 了解详细信息。
所以您应该重命名 POCO 属性 或 JSON 部分名称。在您的情况下 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>();
再补充一点。 Serilog.Events.LogEventLevel
枚举没有 Critical
级别,它有 Error
和 Fatal
。
这就是为什么这个项目
{
"Level": "Critical",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}
将无法正确加载。您应该将其更改为:
{
"Level": "Error",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Error/log-{Date}.log"
}
鉴于这两个 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"));
}
}
和一个看起来像这样的 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"
}
],
}
我想将 SubLogger
部分绑定到 SerilogSubLoggerConfigurations
class。我似乎无法弄清楚如何获得此配置以使用三个项目(SerilogSubLoggerConfiguration
类型)创建 class。
在 Json 配置中,您的子记录器对象数组称为 SubLogger
,但在配置 POCO SerilogSubLoggerConfigurations
中称为 SerilogConfigurations
。
这就是配置活页夹无法匹配它们的原因。
在 ASP.NET Core 中,不可能(至少目前)更改映射属性的名称,它们应该与配置部分的名称相匹配。查看此 answer 了解详细信息。
所以您应该重命名 POCO 属性 或 JSON 部分名称。在您的情况下 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>();
再补充一点。 Serilog.Events.LogEventLevel
枚举没有 Critical
级别,它有 Error
和 Fatal
。
这就是为什么这个项目
{
"Level": "Critical",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Critical/log-{Date}.log"
}
将无法正确加载。您应该将其更改为:
{
"Level": "Error",
"pathFormat": "C:/Logs/Serilog/%APPLICATION_NAME%/Error/log-{Date}.log"
}