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

Ein*_*arI 32 .net c# .net-core asp.net-core

升级到.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!");
    });
}
Run Code Online (Sandbox Code Playgroud)

Tse*_*eng 36

配置日志记录的方式已经改变了一点...推荐的方式(现在这个GitHub问题/公告中已经很好地记录了这个AddLogging方法的配置记录器,例如

services.AddLogging(builder =>
{
    builder.AddConfiguration(Configuration.GetSection("Logging"))
        .AddConsole()
        .AddDebug();
});
Run Code Online (Sandbox Code Playgroud)

并且appsettings.json喜欢

注意

似乎有些人感到困惑,因为该示例仅演示了Console提供程序的配置而不是所有记录器.

LogLevel部分配置所有命名空间(Default键)或特定命名空间的日志记录级别(System覆盖命名空间以其开头的所有类的日志值)System.*.

这是针对Tin ILogger<T>)中使用的类.这允许为此命名空间中的记录器设置高于或低于默认日志记录级别.

{
  "ApplicationInsights": {
    "InstrumentationKey": ""
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information",
      "System": "Warning",
      "Microsoft": "Information"
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,appsettings.json的结构与以前在.NET Core 1.x中的结构不同Logging,而appsettings.json现在的条目中包含logger提供程序名称,这允许您配置每个日志记录提供程序的日志记录级别.

以前,该条目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();
}
Run Code Online (Sandbox Code Playgroud)

更新

如果一个人不想使用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();
});
Run Code Online (Sandbox Code Playgroud)

  • 或只是builder.SetMinimumLevel(LogLevel.Trace);` (2认同)
  • 请注意,使用`AddConfiguration`需要`Microsoft.Extensions.Configuration`包. (2认同)

Shi*_*mmy 8

我花了差不多二十分钟才意识到,因为Configuration.GetSection("Logging")Startup.cs文件中读取appsettings.json文件中"Logging"配置的部分,该文件被配置为.将其更改为更低或更低,修复了问题."Error""Information"

以下是appsettinsg.json文件现在的样子:

{
  "Logging": {
    "IncludeScopes": true,
    "Debug": {
      "LogLevel": {
        "Default": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要了解有关日志记录级别的更多信息(例如in "Information"),请查看链接,链接还提供有关ASP.NET Core日志记录的一般信息.

我只是发布在这里,以防你在使用日志工作时遇到任何麻烦,确保你已经通过那个JSON文件.


Pan*_*eof 6

以上对我无济于事唯一的解决方法是编写一个方法

private void ConfigLogging( ILoggingBuilder builder ) {
    builder.SetMinimumLevel( LogLevel.Trace );
    //... additional configuration...
}
Run Code Online (Sandbox Code Playgroud)

当使用AddLogging扩展方法时,将其写为

services.AddLogging( ConfigLogging );
Run Code Online (Sandbox Code Playgroud)