essential-diagnostics
essential-diagnostics copied to clipboard
RollingFileTraceListener max files automatic cleanup
Add a max files property to RollingFileTraceListener (would also apply to RollingXmlTraceListener) and configure automatic cleanup of files older than this.
Note: This probably needs an alternative logFileCreationSchedule attribute so that the log file names have a predefined pattern that can be used to determine which 'old' files to delete. i.e. find all files in the target directory (customLocation) matching the baseFileName, parse the date portions, and then delete those older than the defined number of days or weeks. (Note that you can't determine if these files actually are old logs, only if their name matches the pattern.)
Comment thread from old Codeplex:
gerdb42 wrote Feb 6, 2013 at 9:14 PM [x]
Parsing for Date Patterns would certainly be the sophisticated approach.
How about a simple number of files to keep out of a list of files sorted by name? The list could be created searching for files matching the baseFileName with dynamic parts replaced by Wildcards.
zijianhuang wrote Jun 2, 2014 at 8:22 AM [x]
While technically this feature could be implemented in the listener, however, it might be more appropriate for the system administrator to write a few batch scripts executed by Windows Task Scheduler to archive and delete old files. I myself had done this practice.
sgryphon wrote Mar 14, 2016 at 8:38 AM [x]
Rather than maximumFiles (with specific schedule pattern), maybe a maximumAge, but would need a lot of checks to make sure don't over-delete. e.g. grab the folder from the pattern, but make sure it only has 'static' tokens (e.g. app name is okay but date/time is not), then for matching files grab a prefix up until the last 'static' token, plus also check the extension (no tokens allowed) matches.
Check for deletes in a separate async thread (so new logging isn't blocked), whenever 'create' is triggered. Make sure to gracefully handle any errors.