ASP.NET Core – How to turn off startup logging

When you launch an ASP.NET Core web app, you may see the following startup logging messages:

info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:12345 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: D:\TestProject\
Code language: plaintext (plaintext)

These messages come from having a console logger. If you’re using Host.CreateDefaultBuilder(), it adds a console logger provider by default.

There are two simple ways to get rid of these logging messages: turn off logging in appsettings.json or remove the default logging providers. I’ll show how to do these two options below.

Option 1 – Turn off logging in appsettings.json

In appsettings.json (or appsettings.Development.json), you can turn off the startup logging by setting Microsoft.Hosting.Lifetime to None:

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "None" } } }
Code language: JSON / JSON with Comments (json)

Note: This is assuming you’re loading in appsettings.json.

Option 2 – Remove the default logging providers

When you use Host.CreateDefaultBuilder(), it adds multiple default logging providers, including a console logger. The console logger is what’s logging the startup messages that you want to remove. Therefore, to get rid of the startup messages, you have to remove the console logger.

To get rid of the console logger, you have to use IWebHostBuilder.ConfigureLogging() and call LoggingBuilder.ClearProviders(), like this:

public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>() .ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders()); }); }
Code language: C# (cs)

This gets rid of the startup logging messages. If you only wanted to get rid of the console logger, then you can add back the other logging providers inside ConfigureLogging().

Leave a Comment