I am evaluating replacing an existing logger in an existing test application. One of my potential candidates is Serilog. The application may start up to 16 threads and each thread is passed in the name of the log file to use. The threads during the test application are started with 3 different log names. I am using Microsoft extensions so that I can replace Serilog with another logger for evaluation.
I need to be able to close, delete and restart the individual log files. The problem I am encountering, is that once I call the CloseAndFlush method, all the logs are closed. Is there some way to individually close a log file in a thread?
My configuration is below. The RunInLogger is an ILogger property that I pass to each thread. If I use Microsoft Extensions, it is a Microsoft ILogger, or if I use the Serilog ILogger is is a serilog ILogger.
var serilogLogger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Map("Name", "Other", (name, wt) => wt.File($"{name}", outputTemplate: outTML))
.CreateLogger();
Log.Logger = serilogLogger;
ILoggerFactory loggerFactory = new LoggerFactory()
.AddSerilog(serilogLogger);
RunInLogger = loggerFactory.CreateLogger("Program");
Each thread is passed the RunInLogger (ILogger) and the name of a file (RunInLogFile) to begin logging, by using the BeginScope method.
using (RunInLogger.BeginScope(new Dictionary<String, object> { { "Name", RunInLogFile } }))
{
RunInLogger.LogInformation(logEntry);
}
I have experimented with using the Serilog ILogger directly, thinking I could add this to my configuration file - .Enrich.FromLogContext() and replace the BeginScope with the Serilog equivalent - LogContext.PushProperty("Name", RunInLogFile). The resulting code I attempted to only close the current log, but still closed all logs.
using (LogContext.PushProperty("Name", RunInLogFile))
{
RunInLogger.Log.Information(logEntry);
Log.CloseAndFlush()
}
Is there a method to call within the LogContext to only close the log associated with my RunInLogFile?