0

I have a project that uses NLog to create and maintain log files. This includes the use of log-rotation/archiving of old log files. However, I've seen that the archiving settings of NLog are not always respected, especially regarding the ArchiveEvery configuration option. Based on this stackoverflow answer, I assume the library checks the last write time for a file to check if it has to archive the current file and start a new one, but not until a new log message is passed to the library.

In my project I have the library configured to archive every minute. This should be fine, as my project logs messages every few seconds, and I expect to see an archived file every minute because the log messages keep coming. However I see inconsistent behavior, with sometimes multiple minutes in between different, but subsequent, archived log files. For example, I currently have the following files on my disk:

Filename              | Last write time
----------------------+------------------
Log.01-06-2017.2.csv  | 1-6-2017 10:42
Log.01-06-2017.3.csv  | 1-6-2017 10:44
Log.01-06-2017.4.csv  | 1-6-2017 10:46
Log.01-06-2017.5.csv  | 1-6-2017 10:47
Log.01-06-2017.6.csv  | 1-6-2017 10:48
Log.01-06-2017.7.csv  | 1-6-2017 10:52
Log.01-06-2017.8.csv  | 1-6-2017 11:01
Log.01-06-2017.9.csv  | 1-6-2017 11:04
Log.01-06-2017.20.csv | 1-6-2017 11:43
Log.01-06-2017.csv    | 1-6-2017 11:46

As you can see, the archived files are not created every minute. As for my NLog config at the moment:

fileTarget.ArchiveNumbering = ArchiveNumberingMode.DateAndSequence;
fileTarget.ArchiveEvery = FileArchivePeriod.Minute;
fileTarget.KeepFileOpen = true;
fileTarget.AutoFlush = true;
fileTarget.ArchiveDateFormat = "dd-MM-yyyy";
fileTarget.ArchiveOldFileOnStartup = true;

I am struggling to get this to work "properly". I write this in parentheses as I don't have much experience with NLog and don't really know how the library behaves. I had hoped to find more information on the NLog wiki page over at GitHub, but I couldn't find the information I needed over there.

Edit

fileTarget.FileName is comprised of a base-folder (storage.Folder.FullName = "C:\ProgramData\\"), a subfolder (LogFolder = "AuditLog"), and the filename (LogFileName = "Log.csv"):

fileTarget.FileName = Path.Combine(storage.Folder.FullName, Path.Combine(LogFolder, LogFileName));

The fileTarget.ArchiveFileName is not set, so I imagine it being the default one. Could it be that specifying the complete path for the FileName is screwing things up? If so, is there a different way to specify a specific folder to put the log files in?

ThaMe90
  • 4,196
  • 5
  • 39
  • 64

0 Answers0