Hide exceptions from Kestrel console window

396 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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();