【发布时间】:2017-11-28 14:25:02
【问题描述】:
我有一个 ApplicationConfigurationSettings 类,我将 appsettings.json 文件中的值绑定到该类。除了 logger LogLevel 值之外,我可以获取所有内容,这很可能是由于 Logging 条目的 json 格式中存在子级别。
我如下绑定类
services.Configure<ApplicationConfigurationSettings>(Configuration.GetSection("ApplicationConfiguration"));
appsettings.json(为简洁起见已删除部分)
{
"ApplicationConfiguration": {
"ConnectionStrings": {
"DevelopmentConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo_Development;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ApplicationIconUrls": {
"MaleUserIcon": "https://machineryrestorations.blob.core.windows.net/publicfiles/images/BestMaleUser_32x32.png",
"FemaleUserIcon": "https://machineryrestorations.blob.core.windows.net/publicfiles/images/BestFemaleUser_32x32"
},
"ApplicationInfo": {
"VersionNumber": "1.0.0",
"Author": "Jimbo",
"ApplicationName": "CustomTemplate",
"CreatedOn": "November 20, 2017"
}
}
}
我的记录器 POCO
public class LoggerSettings
{
public bool IncludeScopes { get; set; }
public KeyValuePair<string,string> LogLevel { get; set; }
}
我确定这是由于活页夹无法将我的 LogLevel 属性与 json 文件中的内容相协调。由于无法更改 json 文件中的日志记录格式,如何更改我的记录器 POCO 以使其正常工作?
这是 json 提供程序在从
检查配置时解决它的方式services.AddSingleton(Configuration);
{[ApplicationConfiguration:Logging:IncludeScopes, False]} {[ApplicationConfiguration:Logging:LogLevel:Default, Warning]}
我似乎无法在类中正确设置该属性,因此它可以正确绑定。
【问题讨论】:
标签: c# dependency-injection configuration asp.net-core-2.0