升级到 .NET Core 2.0(+ASP.NET Core 2.0 后,我似乎无法输出跟踪级别日志信息。
实际上,如果我执行一个dotnet new web
项目并在"启动配置"中添加以下代码,则不会收到任何跟踪或调试日志消息,但会收到两次信息和错误消息。注释掉.AddConsole()
调用将仅输出一次这些(信息和错误( - 建议默认情况下使用控制台提供程序自动配置它。请记住,这是一个"文件 ->新"项目体验,除了我添加的内容外,Program.cs
中根本没有用于日志记录或配置的设置。有人见过东西吗?或者我应该为它注册一个 GitHub 问题。
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 问题/公告中对此有很好的记录(是在AddLogging
方法上配置记录器,例如
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
});
并有一个像这样的appsettings.json
通知
似乎有些人感到困惑,因为该示例仅演示了Console
提供程序的配置,而不是所有记录器。
LogLevel
部分为所有命名空间(Default
键(或特定命名空间(System
覆盖命名空间以System.*
开头的所有类日志记录的默认值。
这适用于T
inILogger<T>
中使用的类(。这允许为此命名空间中的记录器设置高于或低于默认日志记录级别。
{
"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 中的结构不同,appsettings.json
中的Logging
条目现在包含记录器提供程序名称,这允许您配置每个日志记录提供程序的日志记录级别。
以前,appsettings.json
中的条目仅适用于控制台记录器。
或者,现在可以改为在WebHostBuilder
内移动日志记录。
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
,也可以在代码中注册过滤器。
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();
});
我花了将近二十分钟才意识到,由于Configuration.GetSection("Logging")
在Startup.cs文件中读取了appsettings.json文件中的配置中"Logging"
的部分,该文件配置为"Error"
.将其更改为"Information"
或任何更低的内容,解决了该问题。
以下是appsettinsg.json文件现在的外观:
{
"Logging": {
"IncludeScopes": true,
"Debug": {
"LogLevel": {
"Default": "Information"
}
},
"Console": {
"LogLevel": {
"Default": "Information"
}
}
}
}
要了解有关日志记录级别(例如在"Information"
中(的更多信息,请查看此链接,该链接还提供了有关 ASP.NET Core日志记录的一般信息。
我只是在这里发布,以防万一您在使日志记录工作时遇到任何问题,请确保您已经完成了该 JSON 文件。
以上都不适合我 唯一的解决方法是编写一个方法
private void ConfigLogging( ILoggingBuilder builder ) {
builder.SetMinimumLevel( LogLevel.Trace );
//... additional configuration...
}
当使用AddLogging扩展方法时,将其写成
services.AddLogging( ConfigLogging );
以下appsettings.json
结构似乎工作正常:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
},
"Console":
{
"IncludeScopes": true
}
}
}
取自 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1
另外,看看你的启动电话是什么,我发现以下内容对我有用:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Sink(jsonSink)
.Enrich.WithExceptionDetails()
.CreateLogger();
Log.Logger = logger;
}
}