2

In my WPF .netCore 5 app, I'm using three libraries where each library accepts Microsoft.Extensions.Logging.ILogger at initialization. I'm using Serilog and ILoggerFactory. I create three LoggerConfigurations with different WriteTo.File paths.

//licence manager logger
var serilogLM = new LoggerConfiguration()
                    .MinimumLevel.Verbose()
                    .WriteTo.File(
                        $"{LogPathSettings.LicenseManagerLogPath}/LicenseManager {LogPathSettings.ShortDateTime}.log",
                        outputTemplate: "{Timestamp:dd-MM-yyyy HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")                    
                    .CreateLogger();

var licenseManagerLogger = Microsoft.Extensions.Logging.LoggerFactory.Create(l =>
   {
        l.AddSerilog(serilogLM);
   }).CreateLogger("LicenseManager");


//client logger
var serilogClient = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .WriteTo.File($"{LogPathSettings.ClientLogPath}/Client {LogPathSettings.ShortDateTime}.log", outputTemplate: "{Timestamp:dd-MM-yyyy HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
                .CreateLogger();

var clientLogger = Microsoft.Extensions.Logging.LoggerFactory.Create(l=>
  {
       l.AddSerilog(serilogClient);
  }).CreateLogger("RedClient");

The issue I'm having is that latest LoggerConfiguration overwrites all previous ones, so instead of each library using their own logger, all three log files are created, but all libraries write to logger which was created last. I know there is a catch somewhere, but I couldn't find the solution yet.

So my question is how can I create three Microsoft.Extensions.Logging.ILogger instances each using its own Serilog with custom LoggerConfiguration.

So since serilogClient logger is created last, the licenseManagerLogger uses the same WriteTo.File sink path.

All samples that I found are using simple logging or logging to multiple files based on LogLevel which is not the case here.

zmuro
  • 21
  • 3
  • @MichalDiviš you can convey that by upvoting and/or starring the question - you don't add much by putting text under it (will delete this if I see it again and your comment has been removed) – Ruben Bartelink Jun 22 '22 at 10:12
  • Simply create a configuration and a factory for each individual logger. – BionicCode Jun 22 '22 at 19:02
  • Edited my post, since what @BionicCode suggested was my idea to at the beginning, but it doesn't work. Maybe because Serilog.Logger is static and you can only have one configuration? Don't know and it's really frustrating, since this should be a simple task. – zmuro Jun 24 '22 at 06:24
  • @zmuro You should not use the static logger reference. This generally not favorable. You should use dependency injection or manually pass the logger to the respective classes. Avoid static fields and properties is generally a good practice. – BionicCode Jun 24 '22 at 08:33
  • You can use Microsoft ILogger infrastructure as API or facade and serilog as the underlying framework. You could use for example .NET IoC framework to setup the logger and prepare it for injection. – BionicCode Jun 24 '22 at 08:37
  • This way you can easily distribute different logger instances or types (by implementing ILogger). – BionicCode Jun 24 '22 at 08:39

0 Answers0