ASP.NET Core 日志记录过于详细

Dom*_*ikS 6 c# logging azure 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" 
   }
}
Run Code Online (Sandbox Code Playgroud)

还有我的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"));
                });
}
Run Code Online (Sandbox Code Playgroud)

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

我究竟做错了什么?

Tse*_*eng 6

正如selotape所说,.NET Core中设置的loglevel是最低级别。

当您设置时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();
}
Run Code Online (Sandbox Code Playgroud)

// 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)
Run Code Online (Sandbox Code Playgroud)

添加具有三个谓词 ( Func<string, string, LogLevel, bool>Func<string, LogLevel, bool>、 ) 之一的日志记录过滤器,如ASP.NET Core 文档Func<LogLevel, bool>中所示:

过滤功能

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

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();
Run Code Online (Sandbox Code Playgroud)