Create NLog file with current date and time without caching it, keeping the archive file name the same

Brandon picture Brandon · Dec 3, 2014 · Viewed 8.6k times · Source

I'm using NLog to do some logging and I've run into an issue with the archiving and filenames.

I'm creating the logging configurations in code (I'm writing a wrapper to expose some specific functionality), and I've got the FileTarget object created with these options:

this._fileTarget.FileName = "${date:format=yyyy-MM-dd hh.mm.ss}.log";
this._fileTarget.ArchiveAboveSize = Math.Pow(1024, 2) * 5; //5MB
this._fileTarget.ArchiveNumbering = ArchiveNumberingMode.Date;
this._fileTarget.ArchiveEvery = FileArchivePeriod.Day;
this._fileTarget.ArchiveDateFormat = "yyyy-MM-dd hh.mm.ss";

I'm not sure why NLog did it like this, but that file name layout will create a new log file every second instead of just using the file name until the archive period is up.

I added the cached target wrapper to the filename as such:

this._fileTarget.FileName = "${cached:cached=true:inner=${date:format=yyyy-MM-dd hh.mm.ss}.log}";

But all that buys me is a file name that now never changes, and looks like this when archived

2014-12-03 12.00.00.2014-12-03 12.00.00.log
2014-12-03 12.00.00.2014-12-03 12.10.00.log
etc...

Is it possible to tell NLog to only archive files based on the file size and date and to ignore the archive numbering and date formatting? I'm trying to produce log files whose name is the timestamp of when it was created, where new files are only created above a certain size or every day.

Edit: This is what I am trying to acheive:

On application start

  1. Creates log file with the file name in the format of "yyyy-MM-dd hh.mm.ss".txt by using the current time stamp (Example -> "2014-04-29 11:11:11.txt")
  2. The application logs to that file for a while until it either
    • Becomes too large (over a defined limit) or
    • The archive time elapses (in my case, this is one day)
  3. The logger then stops writing to the file created in 1. ("2014-04-29 11.11.11.txt") and does not rename it or otherwise add sequential/rolling numbers or date formatting to it.
  4. Repeat steps 1-3 until the end of time.

Leaving me with a Log folder that looks like this:

Logs
|
|> 2014-12-10 12:50:50.txt    (1024 kb)
|> 2014-12-10 12:50:55.txt    (1020 kb)
|> 2014-12-10 12:51:01.txt    (1024 kb)
|> 2014-12-10 12:51:10.txt    (1003 kb)  
|> 2014-12-10 12:51:20.txt    (400 kb)  <-The currently active log file.

Answer

Julian picture Julian · Nov 30, 2016

This is an old question, but there are some recent improvements for this case.

I'm not sure why NLog did it like this, but that file name layout will create a new log file every second instead of just using the file name until the archive period is up.

Since NLog 4.3.9 you could configure the "cachekey", so you could control when the cache should be invalided.

For this case the following configration give you the date and time in the file name, but only 1 file each day. The time in the file name will the time of the first log event for that day.

filename="${cached:cached=true:Inner=${date:format=yyyy-MM-dd hh.mm.ss}:CacheKey=${shortdate}}.log"