Search code examples
asp.net-core-2.0kestrel-http-serverasp.net-core-logging

Hide exceptions from Kestrel console window


I am using Kestrel with .NET Core 2 to serve a Web API application.

I want hide exceptions that occur from displaying in the console window.


Solution

  • Try built-in Log filtering mechanism.

    You can specify a minimum log level for a specific provider and category or for all providers or all categories. Any logs below the minimum level aren't passed to that provider, so they don't get displayed or stored.

    It should beMicrosoft.AspNetCore.Server.Kestrel category name for such logs. Filter rule may be registered in code via AddFilter extension method for ILoggingBuilder:

    // using Microsoft.Extensions.Logging.Console;
    
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(logging =>
            ...
            logging.AddFilter<ConsoleLoggerProvider>(
                              "Microsoft.AspNetCore.Server.Kestrel",
                              LogLevel.Critical))
        .Build();