2

I've just started using NLog, but am concerned that the answer to a simple problem is seemingly unavailable.

I'd like to create a new log each time my application starts, and archive the previous log, keeping only four logs total.

I've searched quite a bit and, I've found caching. With it I'm able to create a new log on each application start, but there is no answer to archiving the old log.

Any ideas?

Community
  • 1
  • 1
Charles W
  • 2,262
  • 3
  • 25
  • 38

3 Answers3

3

It would be awesome to have the archive on execute flag like Gonzalo Contento suggests... but until then there's a workaround of sorts...

It doesn't quite deal with the archiving setup, but it does get you one log per execution. The archiving should be easy to configure after that... but maybe not.

I'll quote my answer from another similar question here: https://stackoverflow.com/a/30991594/495000


I came across this problem myself, and it took me a long time to figure it out. Most of the answers I've found only have part of the solution and don't explain how/why it works... which makes it hard to adapt if you have a slightly different use case.

Example:

<target name="log"
   xsi:type="File"
   fileName="${basedir}/logs/log.${longdate:cached=true}.log"
   layout="${message}"
   archiveFileName="${basedir}/logs/archives/log.${shortdate}.{#}.log"
   archiveAboveSize="5242880"
   archiveEvery="Day"
   archiveNumbering = "Rolling"
   maxArchiveFiles="20" 
   />

Explanation

You have to use both the Cached Layout Renderer and the longdate variable. To understand why this works, you need to understand how they they work, and how they interact.

longdate:

fileName="${basedir}/logs/log.${longdate}.log"

Using the longdate variable in your log name will pretty much guarantee a new log file on every execution... except it creates a new log file every millisecond even during a single execution which is probably not desirable except in the most rare of circumstances.

Cached Layout Renderer:

fileName="${basedir}/logs/log.${shortdate:cached=true}.log"

Cached layout renderer will cache the variable on the first log call, and then always use that value for subsequent entries... but the cache only persists until the execution completes. Using shortdate, or any other variable that isn't guaranteed to changeon each execution, won't work. It will find a log file with the same filename it wants to use, and it'll just append (or delete if you have that set). This is not what we want.

Combined:

fileName="${basedir}/logs/log.${longdate:cached=true}.log"

This works because it takes the millisecond timestamp of the first log per execution, and then caches it, and always uses that logfile until the execution terminates (clearing the cache). Next time you run it (unless it's the same millisecond... unlikely!) you'll get a new value cached, and a new log file (but only one!).

Community
  • 1
  • 1
Mir
  • 2,429
  • 1
  • 29
  • 34
2

I've just posted a issue on NLog Issue Tracker:
Archive existing log file on application start up

Gonzalo Contento
  • 857
  • 9
  • 21
  • You made this happen, thanks! Banging my head wondering why a machine restart doesn't archive the log files from previous session. – Dan Rayson Mar 09 '18 at 12:39
1

I have been looking for similar thing and I think I have found a simpler solution to the problem. The trick is to use 'processid' layout renderer.

fileName = "${basedir}/${shortdate}/${processid}.log

Every time your application runs, it will be assigned a new process id and above will create a separate log file for each run.

SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
  • This is not a good solution because process IDs are only unique for the lifetime of the process. After the process terminates, the ID can be reused by the system. In addition, if the application runs over night, this will lead to multiple logs. – R. W. Sinnet Sep 28 '16 at 16:06
  • @R.W.Sinnet Agreed that process id is not fully unique. But if you combine the Process Id with date timestamp, it will give you a unique log file name for sure. No two processes can share the id at same time. – SolutionYogi Sep 30 '16 at 15:14