Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ILoggerFactory vs serviceCollection.AddLogging vs WebHostBuilder.ConfigureLogging

I have typical logging requirement for my asp.net core 2.x app:

  • use application insight in production,
  • console and debug logger in development env
  • setup some filters based on category and log level

Now I see at least three different API's to configure the logging:

  1. WebHostBuilder.ConfigureLogging()in Program.cs

    public static void Main(string[] args) {     var webHost = new WebHostBuilder()                        .ConfigureLogging((hostingContext, logging) =>         {             logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));             logging.AddConsole();             logging.AddDebug();             logging.AddAzureWebAppDiagnostics();         })         .UseStartup<Startup>()         .Build();      webHost.Run(); } 
  2. Inject ILoggerFactory to Startup.Configure method:

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) {     loggerFactory.AddConsole();     loggerFactory.AddAzureWebAppDiagnostics();     loggerFactory.AddApplicationInsights(app.ApplicationServices,          (category, level) => level >= (category == "Microsoft" ? LogLevel.Error : LogLevel.Information));     } 
  3. in Startup.ConfigureServices:

    public void ConfigureServices(IServiceCollection services) {     services.AddLogging(logging =>      {         logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));         logging.AddConsole();         logging.AddDebug();         logging.AddAzureWebAppDiagnostics();     } } 

What is the difference between those? When to use which?

like image 345
Liero Avatar asked Jun 07 '18 14:06

Liero


People also ask

What is services AddLogging?

AddLogging(IServiceCollection) Adds logging services to the specified IServiceCollection. AddLogging(IServiceCollection, Action<ILoggingBuilder>) Adds logging services to the specified IServiceCollection.

What is ILoggerFactory C#?

Registers a wrapper logger which provides a common way to filter log messages across all registered ILoggerProviders. CreateLogger(ILoggerFactory, Type) Creates a new ILogger instance using the full name of the given type . CreateLogger<T>(ILoggerFactory)

What is logging in. NET Core?

In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).


2 Answers

The third one use ConfigureServices which is a public method in the WebHostBuilder. And the first one use ConfigureLogging which is one of IHostBuilder's extension method in HostingHostBuilderExtensions.

And they both call the IServiceCollection's extension method AddLogging in LoggingServiceCollectionExtensions under Microsoft.Extensions.Logging package. The AddLogging method first try to add two singleton ILoggerFactory and ILogger<> and an enumerable of LoggerFilterOptions. Then do the action for logging(ILoggingBuilder) which finally calls AddProvider method to add the log providers implemented by these providers(Console, Azure) and calls SetMinimumLevel to add LoggerFilterOptions

The second method directly adds the log providers to LoggerFactory. And these providers are called in LoggerFactory when logging methods are called.

As for orders, the second and third methods are called by WebHostBuilder's UseStartup<TStartup> method.

like image 130
Feiyu Zhou Avatar answered Sep 30 '22 10:09

Feiyu Zhou


In ASP.NET Core 3.x, the first example is now the endorsed option. As explained by the documentation

public static IHostBuilder CreateHostBuilder(string[] args) =>     Host.CreateDefaultBuilder(args)         .ConfigureLogging(logging =>         {             logging.ClearProviders();             logging.AddConsole();         })         .ConfigureWebHostDefaults(webBuilder =>         {             webBuilder.UseStartup<Startup>();         }); 

Another change is that writing logs before completion of the DI container setup in the Startup.ConfigureServices method is no longer supported:

  • Logger injection into the Startup constructor is not supported.
  • Logger injection into the Startup.ConfigureServices method signature is not supported

Logging during the host construction is also not supported (since the DI container is not yet set up), the documentation advises to create a separate logger for that case.

like image 22
Bjorn De Rijcke Avatar answered Sep 30 '22 11:09

Bjorn De Rijcke