2017-11-26 4 views
0

appsettings.json 파일의 값을 바인딩하는 ApplicationConfigurationSettings 클래스가 있습니다. 로깅 LogLevel 값을 제외하고는 모든 것을 얻을 수 있습니다. 로깅 항목의 json 형식에 하위 수준이 있기 때문일 가능성이 큽니다. appsettings에서 POCO로 로깅 구성 바인딩 Asp.Net Core 2

{ 
    "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; } 
} 

내가 확신이 때문이다 (간결함을 위해 제거 된 부품)

services.Configure<ApplicationConfigurationSettings>(Configuration.GetSection("ApplicationConfiguration")); 

appsettings.json을 다음과 같이

나는 클래스를 바인딩 바인더가 LogLevel 속성을 json 파일의 내용과 일치시킬 수 없다는 사실에주의하십시오. json 파일에서 로깅 형식을 변경할 수 없으므로 로거 POCO를 변경하려면 어떻게합니까? {[거짓 IncludeScopes : 로깅 ApplicationConfiguration]} {[ApplicationConfiguration : 로깅 : LogLevel에 : 기본, 경고]}

services.AddSingleton(Configuration); 

에서 구성을 검사 할 때 JSON 공급자가 그것을 해결하는 방법입니다

정확하게 바인딩 할 수 있도록 해당 속성을 클래스에서 올바르게 설정하지 못하는 것 같습니다.

답변

1

JSON 객체는

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

간단한 이유 public KeyValuePair<string,string> LogLevel { get; set; } 속성에 매핑 할 수 없습니다.

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

방법이 하나의 KeyValuePair<string,string>에 매핑해야합니다 무엇 후 시간이 경우, 섹션은 다른 분야로 확장 될 것인가? 물론 단순한 키 - 값 개체가 잠재적으로 매핑 될 수 있지만 구성 바인더가 가정에서 지금까지 가지 않는 경우이 방법으로는 작동하지 않습니다.

강하게 입력 된 POCO에서 키 값의 일부 백으로 이동하려고하기 때문에 좋은 점이라고 생각합니다. 어느 정도는 .net 코어에서 사용 된 강력한 유형의 구성에 대한 전체적인 접근 방식을 평가 절하합니다.

문제를 간단하게 해결할 수 있습니다. 그냥 string 유형의 단일 (이 순간을 위해) 부동산의 DefaultLoggingLevel 클래스를 선언 : 당신은 조금 더 가서 Microsoft.Extensions.Logging.LogLevelDefault 속성의 유형을 설정할 수

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

public class LoggerSettings 
{ 
    public bool IncludeScopes { get; set; } 

    public LoggingLevel LogLevel { get; set; } 
} 

.

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

그것은 같은 간단한 POCO 가지고 잔인한 보일 수있다, 당신은 고급 구성을 위해 그들을 꽤 많이있을 것이다 : 구성 바인더는 정확하게 열거 값 LogLevel.Warning"Warning" 같은 문자열 값을 매핑합니다. 하지만 실제로는 강력하게 유형화되고 명시 적이며 확장 가능합니다.

+0

ty,하지만 내 Serilog 질문에 답한 후에 깨달은 것처럼, 나는 Serilog를 사용하도록 전환했습니다. 나는이 대답이 분명히 그 점에 도움이되도록 입력 된 poco를 사용할 것입니다. – dinotom

+0

@ CodeFuller ... 3 연속으로 가고 싶습니까? 다른 Serilog 질문은 여기에 게시되어 있습니다 ... https : //stackoverflow.com/questions/47540478/serilog-not-getting-application-events-only-coded-logger-events – dinotom

관련 문제