I am trying to fully implement Microsoft Extension Logging (MEL) in a new .Net Core Web API service.
Not have any issues adding/configuring providers but trying see how implement some more granularity in the logging.
The following example creates a SeriLog configuration and adds it and a console logger to logging configuration:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("logs/somelogs.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
// Add services to the container.
builder.Logging.ClearProviders();
//logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
builder.Logging.AddSerilog(dispose: true);
builder.Logging.AddConsole();
Now every logger instance will now log to this.
I was hoping to have default loggers but be able select custom loggers for specific situations. For example using the default MEL LoggerFactory to create an instance based on a registered provider:
var loggerFactory = new LoggerFactory();
var newLogger = loggerFactory.CreateLogger("specialLogger");
The above code simply creates a logger and sets the category based on the name.
In previous projects I've generally implemented a simple abstracted logging interface layer (sitting above NLog) and have the option to select a specific configuration.
Are there any recommendations on how to implement this within MEL by maybe extending LoggerFactory?
Alternatively would create a simple logging service that would be a collection of providers where could create specific instances of loggers based on name:
builder.Services.AddSingleton<ISpecialLoggerService>(loggerService);
TIA
References: https://blog.stephencleary.com/2018/06/microsoft-extensions-logging-part-2-types.html https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/ https://michaelscodingspot.com/logging-in-dotnet/