ASP.NET Core 2.1中appsettings.json的IncludeScopes为何报Expression must be of type object错误
ASP.NET Core 2.1 配置IncludeScopes提示类型错误的解决方案

错误原因
- 该提示是Visual Studio的JSON Schema校验抛出的警告,不会影响项目正常运行,但不符合ASP.NET Core 2.1+的日志配置规范。
- .NET Core 1.x版本允许直接在
Logging根节点下配置布尔类型的IncludeScopes属性,2.1版本更新了appsettings.json的默认Logging配置Schema,Logging根节点下的IncludeScopes要求为object类型,直接写入布尔值就会触发类型不匹配提示。
修复方法
根据项目使用的日志组件选择对应方案:
方案1:使用微软原生日志组件
将IncludeScopes调整到对应日志提供器的配置节点下,或按照新Schema格式配置全局生效,参考配置如下:
{ "Logging": { "LogLevel": { "Default": "Warning" }, "IncludeScopes": true, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" }, "IncludeScopes": false // 单独配置Console提供器的作用域开关,优先级高于全局 } }, "serilog": { "write-to": { "sumologic.url": "http://localhost", "RollingFile.pathFormat": "R\\Document\\API\\Logs-{Date}.log" } } }
方案2:使用Serilog作为日志组件
你的配置中已经添加了Serilog节点,若项目已将Serilog替换为默认日志组件,微软原生Logging节点下的IncludeScopes不会生效,直接删除该配置项即可消除警告。Serilog的作用域开关需要在初始化Serilog时调用IncludeScopes()方法,或在Serilog配置节点中添加对应配置。
内容的提问来源于stack exchange,提问作者David Klempfner
相关产品推荐
相关产品推荐

