自 2.0 起的重大变更
正如 Tseng 在下面评论的那样,这个答案将变成自 2.0 起已过时您可以在此处找到有关此公告的更多信息:https://github.com/aspnet/Announcements/issues/238 https://github.com/aspnet/Announcements/issues/238
问题出在哪里...
根据您的Configure()
方法,我发现一个问题:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); // ⇦ you're not passing the LogLevel!
app.UseMvc();
}
这就是为什么您没有对配置集进行任何更改的原因appsettings.json
文件不工作。
默认行为.AddDebug()
没有任何参数传递的是
添加为 LogLevel.Information 或更高级别启用的调试记录器。
如果您想显式地将其设置为使用特定的最小 LogLevel,那么您可以将其直接传递给AddDebug(ILoggerFactory, LogLevel)
method.
loggerFactory.AddDebug(LogLevel.Trace);
更多信息可以找到here https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.debugloggerfactoryextensions.
将其绑定到您的配置。
方法1:从配置中获取值。
LogLevel foo = this.Configuration.GetSection("Logging:LogLevel")
.GetValue<LogLevel>("Default");
loggerFactory.AddDebug(foo);
方法2:使用LogLevel的内置对象
(故意省略。显然它位于所提供的这两种方法之间。)我宁愿选择极端之一而不是半途而废)
方法3:手动(使用ConfigurationBinder)
奇特的ConfigurationBinder
var obj = new MyObject();
ConfigurationBinder.Bind(_configuration.GetSection("Logging:LogLevel"), obj);
它将映射到一个像这样的对象
public class MyObject
{
public LogLevel Default { get; set; }
public LogLevel System { get; set; }
public LogLevel Microsoft { get; set; }
}
这样你就可以通过:
loggerFactory.AddDebug(obj.Default);
关于节点和 appsettings.json 的特别说明
请注意,配置的分隔符使用:
.
例子:"Logging:LogLevel"
会去:
"Logging": {
"IncludeScopes": false,
"LogLevel": { ⇦⇦⇦⇦⇦ Here
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
日志级别枚举
仅供参考,以下是有效的LogLevel
values:
public enum LogLevel
{
Trace = 0,
Debug = 1,
Information = 2,
Warning = 3,
Error = 4,
Critical = 5,
None = 6,
}
Source:
https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.loglevel#Microsoft_Extensions_Logging_LogLevel https://learn.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.loglevel#Microsoft_Extensions_Logging_LogLevel