2

My company is looking to change from Miscrosoft's Enterprise logging framework to Serilog. I am currently being tasked with changing the logging framework over to Serilog so that it.

  1. Does not change the implementation of any current calls using the our logging class.

  2. Mimics the structure used in our old App.config file so that all of the settings are the same.

I am changing everything over to read from a .json file but I am running into an issue. I need to ensure that someone can go into the appsettings.json file and add a new file to log to.

I.E

"Listeners": {
         "File 1": {
            "fileName": "C:\\logs\\Warning.txt",
            "rollInterval": "Day",
            "rollOnFileSizeLimit": true,
            "rollSize": 2000,
            "filter": "Information"
         },
         "File 2": {
            "fileName": "C:\\logs\\Info.txt",
            "rollInterval": "Day",
            "rollOnFileSizeLimit": true,
            "rollSize": 2000,
            "filter": "Error"
         }
}

This is my current implementation of the serilog logger. I am currently specifying a path based on the severity level and storing it in "path", but it needs to be changed so that I am specifying potentially an array of paths based on the category.

         Serilog.Log.Logger = new LoggerConfiguration()
            .Enrich.WithMachineName()
            .Enrich.WithThreadId()
            .Enrich.WithProcessId()
            .MinimumLevel.Information()
            .WriteTo.Debug()
            .WriteTo.File(
            Settings.GetSetting(path, null),
            outputTemplate: Settings.GetSetting("Logging:Template", null))
            .CreateLogger();
         ILogger log = Serilog.Log.ForContext(Enricher.CategoryContext, Category.ToString());

If someone adds a file to the listeners, then it should be able to output a log to it. This also includes outputting to a database. The problem I am facing is that I cannot see a possible way to do this in a single Serilog configuration.

Additionally, I stumbled upon a NuGet package called Serilog.Sinks.Map which does do what I want, but it is in pre-release, and not suitable for my company's production code.

gibsonsp
  • 93
  • 13
  • https://stackoverflow.com/questions/28292601/serilog-multiple-log-files – gandalf Apr 18 '19 at 16:49
  • That answer does not cover the usecase here. There exists already an out of the box solution for utilizing dates/times for multiple file logs. That is not what I am looking to do here. I am looking to be able to specify any number of files/databases to log to ahead of time from a config file such that the underlying implementation doesn't need to be changed to accommodate it – gibsonsp Apr 18 '19 at 16:54

0 Answers0