1

I am using SeriLog in my .Net6 application.

I am trying to create a serilog json configuration that will help me create a new log folder each day. Then the logs for that day will be created in the corresponding folder

Eg:

  • 20221205/Log_20221205_152358.log
  • 20221206/Log_20221206_051633.log
  • 20221207/Log_20221207_084812.log

My Current config is as below

  "Serilog": {
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log.log",
          "rollingInterval": "Minute",
          "fileSizeLimitBytes": 1000000,
          "rollOnFileSizeLimit": true,
          "retainedFileCountLimit": 30,
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithExceptionDetails" ],
    "Properties": {
      "ApplicationName": "SampleApp",
      "Environment": "Int"
    }
  }
}

By using the current configuration, I am not able to create folders with dates. Kindly help me to resolve the issue

Hari Sankar v m
  • 163
  • 1
  • 12

1 Answers1

1

You can add Serilog.Sinks.Map to your logger configuration, and map the timestamp to the path used by the File sink:

return new LoggerConfiguration()
      .WriteTo.Map(le => new DateTime(le.Timestamp.Year, le.Timestamp.Month, le.Timestamp.Day),
        (day, wt) => wt.File($"./{day:yyyyMMdd}/Log_.log",
                             rollingInterval: RollingInterval.Minute,
                             fileSizeLimitBytes: 10,
                             rollOnFileSizeLimit: true,
                             retainedFileCountLimit: 30,
                             outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:t4}] {Message:j}{NewLine}"),
        sinkMapCountLimit: 1)
     .CreateLogger();

This sink needs to be configured from code because it uses a mapping function, so you can't configure it using XML or JSON.

pier07
  • 121
  • 1
  • 7
  • Thanks for the quick response. This is working for me. But if I give like this then, File splitting will not work. May I know is there any way to do this via json config. Thanks in advance – Hari Sankar v m Mar 09 '23 at 05:58
  • Unfortunately Serilog.Sink.Map can't be configured using JSON. You can replicate all your File Sink config in code, I've just edited my answer to include the updated snippet. – pier07 Mar 10 '23 at 16:07
  • Otherwise try checking [this answer](https://stackoverflow.com/a/63340057/4469761) to see if it helps you getting the config from JSON using Map – pier07 Mar 10 '23 at 16:16
  • @pier07 Is it possible to do the same for MSSQL server? What I mean is to move old records for a period of time into separate tables and keep `dbo.Log` for current arrivals. – nzim Mar 31 '23 at 11:32
  • I've never tried that sink, but you could replace `wt.File(...)` in my previous answer with `wt.MSSqlServer(...)` and specify the table name as `TableName = $"Log{day:yyyyMMdd}"`. In this case, you'll have different tables for each day (Log20230403, Log20230404, etc) – pier07 Apr 03 '23 at 14:32