.NET Core 2.0日志记录是否已损坏?

升级到.NET Core 2.0(+ ASP.NET Core 2.0)后,我似乎无法获得输出的Trace级别日志信息。

事实上,如果我做一个dotnet new web项目并在Startup for Configure中添加下面的代码,我没有得到任何跟踪或调试日志消息,但我得到两次信息和错误消息。 注释掉.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喜欢

 { "ApplicationInsights": { "InstrumentationKey": "" }, "Logging": { "IncludeScopes": false, "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() .UseApplicationInsights() .Build(); host.Run(); } 

更新

如果一个人不想使用appsettings.json ,也可以在代码中注册filter。

 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("System", LogLevel.Error) .AddConsole() .AddDebug(); }); 

我花了差不多二十分钟才意识到,因为Startup.cs文件中的Configuration.GetSection("Logging")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://docs.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; } }