4

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

7
  • … where and how are you using that ConfigureLogging? Just having a method like that will not just disable logging… Commented Feb 6, 2018 at 10:01
  • It is invoked when building the web host, updated answer. Commented Feb 6, 2018 at 10:03
  • It should work like that. You have the question tagged with “serilog”, do you use Serilog? Do you have any other logging-related setup elsewhere? Commented Feb 6, 2018 at 10:08
  • I would like to use Serilog but I am not currently, step 1 is to remove all logging from the framework/CLR/Kestrel - then I will add ONLY serilog Json logging. Commented Feb 6, 2018 at 10:12
  • 1
    Can you run the application actually from the command line isntead of within Visual Studio? – Also, if you cannot share more and cannot reproduce the problem in a minimal reproducible example, then I’m afraid we cannot help you. Commented Feb 6, 2018 at 10:38

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

You mean AddSerilog probably? But even if I do this: logging.AddSerilog(logger, true); logging.SetMinimumLevel(LogLevel.None);, I still get exceptions in my console.
The framework only has a single logger factory that is created by the web host where logging providers are attached to. OP’s way of calling loggerBuilder.ClearProviders() in the web host builder should properly clear all default providers making all logs go into the void. Depending on what UseSerilog() you are referring to, this should not clear existing providers.
@Marcus no; github.com/serilog/serilog-aspnetcore has superseded the older compat lib, and now provides UseSerilog() rather than AddSerilog() - the README over there should help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.