serilog / serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
Apache License 2.0
340 stars 117 forks source link

Add setting to delete existing file on startup #328

Open ramtechjoe opened 4 days ago

ramtechjoe commented 4 days ago

Is your feature request related to a problem? Please describe. It would be very helpful to have a single log file for each time an application runs, and to also be able to archive logs from the previous run.

Describe the solution you'd like A setting could be added to the constructor such as archiveOldFileOnStartup. If set to true the Serilog would delete the current log file. This would trigger OnFileDeleting of any FileLifeCycleHooks

Describe alternatives you've considered Currently I am using RollingInterval.Hour and retainedFileCountLimit = 1. In combination with a FileLifecycleHooks that will move the File to an archive folder

Additional context An issue I see is that currently OnFileDeleting seems to be called after the new file is created. This would not work for the desired outcome. Because you would want the old file deleted prior to creating a new file with the same name.

nblumhardt commented 4 days ago

Thanks for your message. We're not planning to add new rolling policies to this sink at the present time, but https://github.com/serilog/serilog-sinks-file/issues/243#issuecomment-2484795044 may be what you're after. HTH!