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. This logger gets added by default.
There are two simple ways to get rid of these logging messages:
- Turn off logging in appsettings.json.
- Remove the default logging providers.
I’ll show both options below.
Option 1 – Turn off logging in appsettings.json
In appsettings.json (or appsettings.Development.json), set 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.
This disables the startup logging messages.
Option 2 – Remove the default logging providers
The framework adds multiple logging providers by default, including a console logger. This is what outputs the startup messages you’re trying to remove. To get rid of these startup messages, remove the console logger with builder.Logging.ClearProviders() in the initialization code:
var builder = WebApplication.CreateBuilder(args);
// rest of adding services
builder.Logging.ClearProviders();
var app = builder.Build();
// rest of init code
Code language: C# (cs)
This gets rid of the startup logging messages. It gets rid of all of the default logging providers. You can re-add the loggers you want.
Clearing logging providers before .NET 6
They simplified the initialization code in .NET 6. Before .NET 6, clear the logging providers in Host.CreateDefaultBuilder() 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)