如何在ASP.NET Core中启用跟踪日志记录?

我无法在我的应用程序中获得basice LogTrace(...)输出。 这是一个复制品:

  1. 使用Visual Studio 2017创建新的ASP.NET Core应用程序。
  2. (可选)注释掉.UseApplicationInsights()以使.UseApplicationInsights()更清晰
  3. 用这个替换ValuesController.cs的代码:

     using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace WebApplication1.Controllers { [Route("api/[controller]")] public class ValuesController : Controller { private readonly ILogger logger; public ValuesController(ILogger logger) { this.logger = logger; } [HttpGet] public IEnumerable Get() { logger.LogError("ERROR!"); logger.LogWarning("WARN!"); logger.LogInformation("INFO!"); logger.LogTrace("TRACE!"); return new string[] { "value1", "value2" }; } } } 
  4. appsettings.Development.json更改为:

     { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Trace", "System": "Information", "Microsoft": "Information" } } } 
  5. 运行并查看Debug输出

这导致:

  • 实际产量:

    只有ERROR,WARN和INFO

  • 预期的产出将是“追踪!” 消息也是

我也试过调整appsettings.json文件中的值,但这也没有效果。

奇怪的是,将任一文件中的值更改为"Error"也没有做任何事情。

底线/问题

我需要做些什么才能使我注入的ILogger尊重日志记录设置,包括Trace级别?


脚注

以下是使用上述repro自动生成的一些相关代码:

Startup.cs

 public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); } } 

Program.cs

 public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .UseApplicationInsights() .Build(); host.Run(); } } 

appsettings.json默认值:

 { "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } } } 

突破2.0的变化
正如Tseng在下面评论的那样,这个答案将在2.0之后变得过时,你可以在这里找到更多关于这个公告: 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文件中的配置集的appsettings.json都不起作用的原因。

没有传递任何参数的.AddDebug()的默认行为是
添加为LogLevel.Information或更高版本启用的调试记录器。

如果要将其显式设置为使用特定的最小LogLevel,则可以将其直接传递给AddDebug(ILoggerFactory, LogLevel)方法。

 loggerFactory.AddDebug(LogLevel.Trace); 

更多信息可以在这里找到。


将其绑定到您的配置。

方法1:从配置中获取值。

 LogLevel foo = this.Configuration.GetSection("Logging:LogLevel") .GetValue("Default"); loggerFactory.AddDebug(foo); 

方法2:使用LogLevel的内置对象

(故意遗漏。很显然,它提供了这两种方法之间的紧密关系。)我赞成其中一个极端,而不是半途而废

方法3:Go Manual(使用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枚举

仅供参考,以下是有效的LogLevel值:

 public enum LogLevel { Trace = 0, Debug = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, None = 6, } 

资源:
https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.extensions.logging.loglevel#Microsoft_Extensions_Logging_LogLevel

这对我有用。 将它添加到ConfigureServices(IServiceCollection services)方法中:

 services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));