I am having an issue formatting the date in the filename using the appsettings.json file for a C# worker service.

My current config looks like this:

Serilog": { "WriteTo": [ { "Name": "File", "Args": { "path": "C:\\Program Files (x86)\\Alarm\\AlarmServiceLogs\\AlarmService.txt", "rollingInterval": "Day" } } ] }

It's very basic. I tried adding {Date: yyyy-DD-MM} and other variations as well as using pathFormat rather than path, but that all just seems to add the verbatum text to the log file name.

Is this doable from the .json file? Otherwise I will do it directly in code as I have with other logs. Just trying to be consistent with my log file naming.

marc_s's user avatar

marc_s

760k186 gold badges1.4k silver badges1.5k bronze badges

Nathan's user avatar

1

First you have to make sure you configured you application to load the config correctly:
Example from Serilog:

static void Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true) .Build(); var logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .CreateLogger(); logger.Information("Hello, world!"); }

If you did this you have to correctly structure your json like this: https://github.com/serilog/serilog-sinks-rollingfile?tab=readme-ov-file#controlling-event-formatting

{ "Serilog": { "WriteTo": [ { "Name": "RollingFile", "Args": { "pathFormat": "log-{Date}.txt" } } ] } }

Changer's user avatar

2 Comments

Your example here is using the rolling file sink, which has been deprecated for a long time. Instead, the file sink should be used, which supports rolling functionality.

2025-11-26T14:07:26.6Z+00:00

Do you have a code sample for that?

2025-11-27T10:18:42.103Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.