ASP.NET Core日志记录太冗长

时间:2018-11-24 10:51:41

标签: c# azure logging asp.net-core

我在ASP.NET Core 2.1 WebApi应用程序中的配置日志记录方面遇到麻烦。我成功实现了将消息记录到Azure并在日志流中检查消息,但是这些日志太冗长。我不想将来自类别Microsoft.AspNetCore的消息记录在信息级别中。这是我在appsettings.json文件中的日志记录部分:

"Logging": {
  "LogLevel": {
    "Default": "Debug",
    "Microsoft.AspNetCore.Hosting.Internal.WebHost": "Debug",
    "GameHub": "Information" 
   }
}

还有我的Program类:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("appsettings.json", optional:true, reloadOnChange:true); })
            .ConfigureLogging((ctx, logging) =>
                {
                    logging.AddConfiguration(ctx.Configuration.GetSection("Logging"));
                });
}

它仍以信息级别而不是调试级别记录来自类别Microsoft.AspNetCore的消息。

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

信息级别高于 (或更低,具体取决于您如何看待)调试级别,因此 less 冗长。如果将其设置为“调试”,则还将获得所有信息(以及警告,错误)。我怀疑您可以在标准Core日志记录中更改它。


作为参考,大多数日志框架(从最详细到最不详细)的日志级别为:

详细, 调试, 信息, 警告, 错误, 致命的

(某些框架使用的命名稍有不同,正如Tseng在下面的注释中正确指出的,但基本思想保持不变)。

在某些框架上-例如log4net-您可以设置最大和最小级别,但是带有Core内置日志记录的AFAIK只能设置最小级别,因此也可以使所有级别都超过该级别。

答案 1 :(得分:2)

如Sellotape所说,.NET Core中设置的日志级别是最低级别。

设置Debug时,它将记录Critical, Error, Warning, Information, Debug的级别。它不会记录Trace(详细程度最高)。

如果您不希望使用Information,请将其设置为Warning,则只会记录Critical, Error, Warning

但是,如果您想要Critical, Error, Warning, Debug没有信息,则不能直接使用appsettings.json来做到这一点。

public static class Program
{
    public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) => { ... })
            .ConfigureLogging((webhostContext, builder) => {
                builder.AddConfiguration(webhostContext.Configuration.GetSection("Logging"))
                .AddFilter<ConsoleLoggerProvider>(logLevel => logLevel!=LogLevel.Information)
                .AddConsole()
                .AddDebug();
            })
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights();
}

// by strong typedProvider
.AddFilter<ConsoleLoggerProvider>(logLevel => logLevel!=LogLevel.Information)
// or by name
.AddFilter("Console", logLevel => logLevel != LogLevel.Information)
// or generic/global
.AddFilter(logLevel => logLevel != LogLevel.Information)

添加日志过滤器,其中包含三个谓词(Func<string, string, LogLevel, bool>Func<string, LogLevel, bool>Func<LogLevel, bool>)之一,如ASP.NET Core Documentation所示:

  

过滤器功能

     

为所有没有通过配置或代码分配规则的提供程序和类别调用过滤器功能。函数中的代码可以访问提供程序类型,类别和日志级别。例如:

WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureLogging(logBuilder =>
    {
        logBuilder.AddFilter((provider, category, logLevel) =>
        {
            if (provider == "Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider" && 
                category == "TodoApiSample.Controllers.TodoController")
            {
                return false;
            }
            return true;
        });
    })
    .Build();