【问题标题】:Binding logging configuration from appsettings to POCO Asp.Net Core 2从 appsettings 绑定日志配置到 POCO Asp.Net Core 2
【发布时间】: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


    【解决方案1】:

    Json 对象

    "LogLevel": {
      "Default": "Warning"
    }
    

    无法映射到public KeyValuePair&lt;string,string&gt; LogLevel { get; set; } 属性,原因很简单。如果一段时间后,该部分将扩展为其他字段怎么办:

    "LogLevel": {
      "Default": "Warning",
      "SomeOtherField": "SomeValue"
    }
    

    应该如何映射到单个KeyValuePair&lt;string,string&gt;?当然,在您的简单情况下,可能会映射此类单个键值对象,但配置绑定器的假设并没有走得太远,它只是不能以这种方式工作。

    而且我相信这是一件好事,因为您正试图从强类型 POCO 转变为一些键值包,它在某种程度上贬低了 .net 核心中采用的强类型配置的整个方法。

    解决您的问题非常简单。只需使用 string 类型的单个(暂时)属性 Default 声明 LoggingLevel 类:

    public class LoggingLevel
    {
        public string Default { get; set; }
    }
    
    public class LoggerSettings
    {
        public bool IncludeScopes { get; set; }
    
        public LoggingLevel LogLevel { get; set; }
    }
    

    您可以更进一步,将Default 属性的类型设置为Microsoft.Extensions.Logging.LogLevel。配置绑定器将正确地将字符串值(如"Warning")映射到枚举值LogLevel.Warning

    public class LoggingLevel
    {
        public LogLevel Default { get; set; }
    }
    

    拥有如此简单的 POCO 似乎有点矫枉过正,您将拥有相当多的 POCO 用于高级配置。但这实际上是要走的路,强类型、显式和可扩展。

    【讨论】:

    • ty,但正如您在回答我的 Serilog 问题后可能已经意识到的那样,我转而使用 Serilog。我将在那里使用打字的 poco,所以这个答案在这方面肯定对我有帮助
    • @CodeFuller...想连续三个?我在这里发布了另一个 Serilog 问题...stackoverflow.com/questions/47540478/…
    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2019-09-25
    • 2018-12-27
    相关资源
    最近更新 更多