Marcus
Marcus

Reputation: 8669

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

Upvotes: 4

Views: 3524

Answers (2)

Raj
Raj

Reputation: 1284

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

Upvotes: 3

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31832

The framework has a default ILoggerFactory that it will use if none is registered.

When you add UseSerilog(), this will replace the default factory, and the default console logging will go away.

Upvotes: 0

Related Questions