How to Turn Off Startup Logging in ASP.NET Core

By FoxLearn 2/4/2025 8:24:29 AM   9
When you launch an ASP.NET Core web application, you might notice several startup log messages in the console.

These log entries typically include:

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\

These messages are generated by the default console logger included with ASP.NET Core. If you'd like to disable or suppress these startup logs, there are two main approaches you can take:

1. Turn Off Logging in appsettings.json

One simple way to suppress startup logs is by configuring logging levels in the appsettings.json file (or appsettings.Development.json, depending on your environment). You can set the logging level for Microsoft.Hosting.Lifetime to None, which will disable these specific messages.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "None"
    }
  }
}

This configuration assumes that you're using appsettings.json in your project. By setting "Microsoft.Hosting.Lifetime": "None", you effectively silence those startup logs, but leave other logs intact.

2. Remove the Default Logging Providers

Another way to remove the startup messages is to eliminate the console logger entirely. ASP.NET Core includes several default logging providers, including the console logger that outputs the startup logs. By clearing these providers, you can suppress these messages.

To remove the default logging providers, you can add the following line of code to your application's initialization routine:

var builder = WebApplication.CreateBuilder(args);

// Remove all default logging providers, including the console logger
builder.Logging.ClearProviders();

var app = builder.Build();

// Other application setup code

This will remove all logging providers, not just the console logger. If you want, you can selectively add back specific logging providers (such as file logging, database logging, etc.) after clearing the default ones.

Prior to .NET 6, the initialization code was a bit more complex. You had to clear the logging providers in the Host.CreateDefaultBuilder() method like so:

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());
            });
}

This code is functionally equivalent to the .NET 6 approach but was the standard prior to the simplified model introduced in .NET 6.

Both methods are effective for turning off startup logging in ASP.NET Core applications. Option 1 is a simple configuration-based solution, while Option 2 gives you more control by allowing you to clear all logging providers, offering a more customizable logging setup.