Without using a dependency injection, it was possible to create a logger using the LoggerFactory, such as
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole().AddDebug();
ILogger<Program> logger = loggerFactory.CreateLogger<Program>();
As the extension methods for ILoggerFactory are now obsolete, how can a logger created without a DI container?
This is also being discussed in Entity Framework Core, see Revisit user logging configuration patterns with EF Core: https://github.com/aspnet/EntityFrameworkCore/issues/14136
LoggerFactory.Create method is now available (in 3.0) so you can do:
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddConfiguration(loggingConfiguration.GetSection("Logging"))
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("SampleApp.Program", LogLevel.Debug)
.AddConsole()
.AddEventLog();
});
The only documentation for logging that I can find is Logging in ASP.NET Core. It seems that Microsoft.Extensions.Logging is being used in numerous packages that sit outside of the "ASP" or "web based" side of things. Documentation is severely lacking for creating an ILoggerFactory when one is not using the "ASP pipeline" as many, I assume, .NET Core apps do not.
Am I looking in the wrong places or is there a reason for this lack of information?
It's nice that we can do LoggerFactory.Create in .net core 3.0, but that won't release for quite some time still. What's the recommended way to achieve this in the meantime, for those of us who are still stuck on 2.2? Should we just ignore the warnings until 3.0 releases?
Most helpful comment
It's nice that we can do LoggerFactory.Create in .net core 3.0, but that won't release for quite some time still. What's the recommended way to achieve this in the meantime, for those of us who are still stuck on 2.2? Should we just ignore the warnings until 3.0 releases?