Skip to content

Added support for customising date in filename #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

RandomTIP
Copy link

Address (Partially): #243

Changes:

  • PathRoller now recognizes and uses {Date} placeholder in file name.
  • Regex matching and directory search patterns have been adjusted for use with {Date} placeholder

This update should Enable users to use following configuration:

var config = new LoggerConfiguration()
            .WriteTo.File("{Date}-log.txt", rollingInterval: RollingInterval.Day);
var log = config.CreateLogger();

Unit Tests Added and Updated in:

  • TemplatePathRollerTests
  • RollingFileSinkTests

…aceholder.

 This commit simply adds support to include date at the start (instead of the end) of filename whenever needed.
 Relevant Tests were also added For PathRoller.cs in TemplatedPathRollerTests.cs

 This commit partially addresses serilog#243
@RandomTIP
Copy link
Author

I know this PR came out of nowhere, but I needed support for this for my project and thought I would send a PR to the original repo as well since the changes are quite simple and straightforward.

This partially addresses #243 without any sort of high level customization, I wanted to make it as simple as possible because, after one or two days worth of searching, I think most of the requests were only related to Date customization in filename.

I would appreciate and take into considaration any and all notes on this one

@RandomTIP RandomTIP changed the title Added support for customising date in filename by including Added support for customising date in filename Mar 10, 2025
@nblumhardt
Copy link
Member

Thanks @RandomTIP. This functionality is something we specifically decided to leave behind in Serilog.Sinks.RollingFile - using that package is the way to go, if you need {Date}, but otherwise, using your own fork of this sink is going to be the way forwards. Thanks again!

@RandomTIP
Copy link
Author

Thanks for the information. I was not able to gather that much myself for some reason... I assumed Serilog.Sinks.RollingFile was deprecated and that is why I overlooked it.

@nblumhardt
Copy link
Member

It is deprecated, but still works; grabbing the RollingFileSink class from there, but modifying it to use Serilog.Sinks.File's FileSink/SharedFileSink should cover a lot of the implementation details and result in a fully-maintained option. HTH!

@nblumhardt nblumhardt closed this Mar 13, 2025
@BernhardMarconato
Copy link

@nblumhardt I need a very similar feature. I tried copying the Serilog.Sinks.File.RollingFileSink to my project to create a fork, but a lot of the members used are internal, making it hard to have a 1:1 copy just with a different PathRoller.

Wouldn't it be possible to make PathRoller implement a public IPathRoller interface, and let users provide a custom IPathRoller implementation? I don't think this would cause much additional maintenance effort from your side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants