.NET Core 2.0日志记录是否损坏? [英] Is .NET Core 2.0 logging broken?
问题描述
升级到.NET Core 2.0(+ ASP.NET Core 2.0)后,我似乎无法获得跟踪级别的日志信息。
I can't seem to get Trace level log information outputted after upgrading to .NET Core 2.0 (+ASP.NET Core 2.0).
实际上,如果我执行了一个 dotnet新网络
项目,并在启动用于配置中添加了以下代码,但没有得到任何跟踪或调试日志消息,但是两次获得了信息和错误消息。 。注释掉 .AddConsole()
调用将只输出一次(信息和错误)-建议默认情况下使用控制台提供程序对其进行自动配置。请记住,这是文件->新建项目的经验,在 Program.cs
中没有任何设置可用于为此进行日志记录或配置-除了我所做的之外已添加。有人看过东西吗?还是应该为其注册GitHub问题。
In fact, if I do a dotnet new web
project and add the code below in Startup for Configure, I do not get any trace or debug log messages, but I get the Information and Error messages twice. Commenting out the .AddConsole()
call will output these (Information and Error) only once - suggesting that it gets configured automatically with a console provider by default. Keep in mind, this is a "File -> New" project experience, there is nothing setup in Program.cs
for logging or configuration at all for this - except for what I've added. Anyone seen things? Or should I register a GitHub issue for it.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Microsoft.Extensions.Logging.LogLevel.Trace);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var logger = loggerFactory.CreateLogger("Blah");
logger.LogTrace("Hello world : Trace");
logger.LogDebug("Hello world : Debug");
logger.LogInformation("Hello world : Information");
logger.LogError("Hello world : Error");
await context.Response.WriteAsync("Hello World!");
});
}
推荐答案
配置日志的方式有改变了一点...推荐的方式(此GitHub问题/公告中有很好的记录 a>现在要做的是使用 AddLogging
方法配置记录器,例如
The way logging is configured has changed a little... The recommended way (and it's pretty well documented in this GitHub issue/announcement to do it now is to configure the loggers on the AddLogging
method, such as
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
});
并有一个 appsettings.json
似乎有些人感到困惑,因为仅是示例演示了控制台
提供程序的配置,而不是所有记录器。
Seems a few people are confused, because the example only demonstrates the configuration of Console
provider and not all loggers.
LogLevel
部分为所有名称空间( Default
键)或特定名称空间配置日志记录级别( System
会覆盖其名称空间以 System开头的所有类的默认日志记录。*
。
The LogLevel
section configures logging level for all namespaces (Default
key) or for a specific namespace (System
overrides the default value for all classes logging whose namespace starts with System.*
.
这是针对 ILogger< T>
中 T
中使用的类的。这允许为此名称空间中的记录器设置高于或低于默认记录级别。
This is for the class used in T
in ILogger<T>
). This allows to set a higher or lower than default logging level for loggers from this namespace.
{
"ApplicationInsights": {
"InstrumentationKey": ""
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Information"
},
"Console": {
"LogLevel": {
"Default": "Warning",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
请注意,appsettings.json的结构与.NET Core 1.x中的结构有所不同,而<$ c现在, appsettings.json
中的$ c> Logging 条目中包含记录程序提供程序名称,可让您配置每个记录程序提供程序的记录级别。
Please note that the structure of the appsettings.json changed from what it used to be in .NET Core 1.x and that Logging
entry in the appsettings.json
now has logger provider names in it, which allows you to configure logging levels per logging provider.
以前, appsettings.json
中的条目仅适用于控制台记录器。
Previously, the entry in appsettings.json
would only be applicable to the console logger.
或者,现在可以在 WebHostBuilder
中移动日志记录。
Alternatively, the logging can now be moved within the WebHostBuilder
instead.
public static void Main()
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("hosting.json", optional: false)
.AddEnvironmentVariables();
})
.ConfigureLogging((webhostContext, builder) => {
builder.AddConfiguration(webhostContext.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
})
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
更新
如果不想使用 appsettings.json
,也可以在代码中注册过滤器。
Update
In case one doesn't want to use the appsettings.json
, one can register the filters in code too.
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
// filter for all providers
.AddFilter("System", LogLevel.Debug)
// Only for Debug logger, using the provider type or it's alias
.AddFilter("Debug", "System", LogLevel.Information)
// Only for Console logger by provider type
.AddFilter<DebugLoggerProvider>("System", LogLevel.Error)
.AddConsole()
.AddDebug();
});
这篇关于.NET Core 2.0日志记录是否损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!