1

I have a bunch of archives, each contains thousands of files to be processed. I want to write one log file as summary, one containing errors if any, and finally, for each archive, a separate log file with all entries processed.

The first two can be configured with WriteTo and Filters. But I don't know how many archives up front. Is it possible to start new log files at runtime in a loop?

Thanks

Whoever
  • 1,295
  • 1
  • 14
  • 21

1 Answers1

2

Yes, Serilog loggers can be created and destroyed on-the-fly:

foreach (var archive in archives)
{
    var filename = "archive-" + archive.Name + ".txt";
    using (var log = new LoggerConfiguration()
            .WriteTo.File(filename)
            .CreateLogger())
    {
        log.Information("Processing archive");
        // etc.
    }
}
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101