Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a LoggerFactory with a ConsoleLoggerProvider?

The ConsoleLoggerProvider has four constructors:

  1. ConsoleLoggerProvider(IConsoleLoggerSettings)
  2. ConsoleLoggerProvider(IOptionsMonitor<ConsoleLoggerOptions>)
  3. ConsoleLoggerProvider(Func<String,LogLevel,Boolean>, Boolean)
  4. ConsoleLoggerProvider(Func<String,LogLevel,Boolean>, Boolean, Boolean)

Three of them are declared obsolete with this message:

This method is obsolete and will be removed in a future version. The recommended alternative is using LoggerFactory to configure filtering and ConsoleLoggerOptions to configure logging options.

With constructor #3, creating a LoggerFactory with a ConsoleLoggerProvider is straightforward (as documented on Entity Framework Core - Logging):

var loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider((category, level) => level >= LogLevel.Information, true) }); 

But since it's deprecated, we are left with constructor #2. Here's what I found to be equivalent:

var configureNamedOptions = new ConfigureNamedOptions<ConsoleLoggerOptions>("", null); var optionsFactory = new OptionsFactory<ConsoleLoggerOptions>(new []{ configureNamedOptions }, Enumerable.Empty<IPostConfigureOptions<ConsoleLoggerOptions>>()); var optionsMonitor = new OptionsMonitor<ConsoleLoggerOptions>(optionsFactory, Enumerable.Empty<IOptionsChangeTokenSource<ConsoleLoggerOptions>>(), new OptionsCache<ConsoleLoggerOptions>()); var loggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider(optionsMonitor) }, new LoggerFilterOptions { MinLevel = LogLevel.Information }); 

This seems overly complicated, am I missing something simpler?

like image 396
0xced Avatar asked Dec 09 '18 09:12

0xced


People also ask

What is the use of ILogger in. net Core?

ILoggerFactory is a factory interface that we can use to create instances of the ILogger type and register logging providers. It acts as a wrapper for all the logger providers registered to it and a logger it creates can write to all the logger providers at once.

What is Microsoft extensions logging?

Microsoft. Extensions. Logging is an extensible logging mechanism with plug-in providers for many common logging systems.

What is Loggerfactory in .NET core?

It is designed as a logging API that developers can use to capture built-in ASP.NET logging as well as for their own custom logging. The logging API supports multiple output providers and is extensible to potentially be able to send your application logging anywhere.

What is ILogger C#?

The responsibility of the ILogger interface is to write a log message of a given log level and create logging scopes. The interface itself only exposes some generic log methods which are then used by “external” extension methods like LogInformation or LogError .


1 Answers

In .NET Core 2.2, you can build an ILoggerFactory without using obsolete methods through Microsoft's dependency injection framework. It's a little less verbose than the version where everything is constructed by hand. Here’s how:

using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;  IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(builder => builder     .AddConsole()     .AddFilter(level => level >= LogLevel.Information) ); var loggerFactory = serviceCollection.BuildServiceProvider().GetService<ILoggerFactory>(); 

And in .NET Core 3.0, you can use LoggerFactory.Create:

var loggerFactory = LoggerFactory.Create(builder => {         builder.AddFilter("Microsoft", LogLevel.Warning)                .AddFilter("System", LogLevel.Warning)                .AddFilter("SampleApp.Program", LogLevel.Debug)                .AddConsole();     } ); 

See also:

  • Migrate from Microsoft.Extensions.Logging 2.1 to 2.2 or 3.0
like image 155
0xced Avatar answered Oct 13 '22 10:10

0xced