nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Add option to specify wildcard in DetermineLastFileLogName() #54

Open dowdybrown opened 1 year ago

dowdybrown commented 1 year ago

Could an option be added to specify the wildcard used by DetermineLastFileLogName()? Line 166 of FileLoggerProvider.cs has a hard-coded "*". In my use case, I have MaxRollingFiles set to 3. So the log files should be basename, basename1 and basename2. The wildcard on line 166 would best be a "?" in my case. The problem with "" is that there may be other log files that have been saved off for later investigation in the folder that "\" picks up, but "?" would filter out. As it is now, the code overwrites those log files that we had saved for later investigation. Work-around is to add a prefix to the filename, change the extension, or move them to a different folder.

VitaliyMF commented 1 year ago

Regarding

Line 166 of FileLoggerProvider.cs has a hard-coded "*". In my use case, I have MaxRollingFiles set to 3. So the log files should be basename, basename1 and basename2. The wildcard on line 166 would best be a "?" in my case.

This code finds 'the latest' log file name for case when 'FileSizeLimitBytes' is set and log file name is not the only log file. It is assumed that the directory contains only log files generated by FileLoggerProvider, and ? is not suitable here because MaxRollingFiles can be 2-digit, 3-digit etc.

If this is not true in your case, I may recommend to use FormatLogFileName handler to generate custom log file names and/or control its folder. Note that FormatLogFileName is called often (each time when FileLoggerProvider is about to flush log messages to a file) and if code here adds a significant overhead the result should be cached somehow (this is up to you and totally depends on the logic inside FormatLogFileName handler).