DEV Community

manoj
manoj

Posted on

Default logging providers in ASP.NET Core

Main() method in the Program class in Program.cs file is the entry point for an asp.net core application. This method calls CreateDefaultBuilder() method performs several tasks like

  1. Setting up the web server
  2. Loading the host and application configuration from various configuration sources and Configuring logging

The following is the code snippet from CreateDefaultBuilder() method.

.ConfigureLogging((hostingContext, logging) =]
{
    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    logging.AddConsole();
    logging.AddDebug();
    logging.AddEventSourceLogger();
})
Enter fullscreen mode Exit fullscreen mode

As part of configuring logging, CreateDefaultBuilder() method adds the following 3 logging providers by default. This is the reason when we run the asp.net core project we see the log displayed both on the console and on the debug window in Visual Studio.

  1. Console
  2. Debug
  3. EventSource

CreateDefaultBuilder() method looks for Logging section in the application configuration file appsettings.json.

Logging section in appsettings.json

The following is the Logging section in appsettings.json file on my machine.

"Logging": {
  "LogLevel": {
    "Default": "Warning",
    "Microsoft": "Information"
  }
}
Enter fullscreen mode Exit fullscreen mode

LogLevel is used to control how much log data is logged or displayed.

Top comments (0)