Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely remove console logging from .NET Core 2.0 Web API

I did this:

public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseUrls("http://0.0.0.0:5000")
        .ConfigureLogging(ConfigureLogging)
        .Build();
}

private static void ConfigureLogging(WebHostBuilderContext hostingContext, ILoggingBuilder logging)
{   
    logging.ClearProviders();
}

And my appsettings.json:

{

}

But still.. I get exceptions logged to Console - can somebody explain why? Pointers?

Exception is still logged to Console

like image 375
Marcus Avatar asked Sep 14 '25 23:09

Marcus


1 Answers

You can configure the logging while creating the webhost like this

 private static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
                .ConfigureLogging(config => {
                    config.ClearProviders();
                })
                .UseKestrel()
                .UseStartup<Startup>();

The config.ClearProviders() will remove all logging service and only produce minimal information

like image 136
Raj Avatar answered Sep 17 '25 14:09

Raj