0

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?

  • Why aren't you using a ILogger instance per thread? You can call CloseAndFlush() on it without effecting others. – esskar Oct 09 '20 at 20:35
  • Thanks for the response. My main application starts all the threads and events are used to notify the thread to discontinue the current list of tests. After a thread stops or finishes, the application logs some results in each log. It may be possible to move the result logging into each thread, and remove some of the dependency on each thread and the application. I will prototype and let you know if that solution works better than my current solution. – Chris Cuciak Oct 09 '20 at 22:55
  • You can still create all instances in the main thread - at the same place where you create your threads, pass the ilogger to the thread. if your main thread needs to log to each thread log, just do it in a loop – esskar Oct 10 '20 at 05:54
  • I already pass in ILogger. The method Log.CloseAndFlush() is a static method for the application. There is no ILogger method to CloseAndFlush(). So anytime I call Log.CloseAndFlush, all log files are closed. – Chris Cuciak Oct 12 '20 at 18:44

0 Answers0