3

We use NLog in our C# service for the logging. File naming is like this:

        <target name="StdLogFile"
                        xsi:type="File"
                        fileName="${LogFolder}/the-app.log"
                        layout="${longdate} [${uppercase:${level}}] ${message}           @(${logger})"
                        archiveEvery="Day"
                        archiveNumbering="Date"
                        archiveDateFormat="yyyy-MM-dd"
                        archiveFileName="${LogFolder}/the-app_{#}.log"
                        maxArchiveFiles="14"
                        autoFlush="true" />

that is:

  • Current logfile will be the-app.log
  • Archived logs will be the-app_2015-03-10.log

We now had the problem, that due to some tests, a fiel the-app_x.log was in the log folder. This prevented log rotation and NLog simply stopped logging to file at all.

Details:

The log folder contained:

  • the-app.log from a previous day
  • the-app_x.log from the same previous day
  • the-app_2015-..-.. etc. older archived files

NLog failed to rotate the existing the-app.log to the archived name TRDS-Import_2015-03-10.log and did not append to the existing the-app.log.

Renaming "the-app_x.log" to "X the-app_x.log" fixed the issue, that is NLog then did rename and create a new logfile for today.

How can we:

a) Make sure log rotation always works?

b) Make the application error and quit if opening the logfile fails?

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • 1
    hope this helps http://stackoverflow.com/questions/8036681/nlog-rotate-and-cleanup-logfiles – user3240361 Mar 11 '15 at 11:26
  • @user3240361 - No, it does not help. My config is OK, it does what it's supposed to do, *unless* there' a "rogue" file in the logs folder. (And a rather inconspicuous one at that.) I can't quite see what this has to do with the linked, question, sorry. – Martin Ba Mar 11 '15 at 11:53
  • Just to get some clarification, this happens only when a file appears the the logging directory that does not match your naming scheme? Does this happen in any other situations? – JNYRanger Mar 11 '15 at 15:03
  • @JNYRanger: I can't say "only", I can definitely say that the presence of the file with the `_x.log` postfix instead of a proper date postfix messed it up. Renaming this file to something else fixed it, i.e. the old log was rotated and a new logfile created. – Martin Ba Mar 11 '15 at 15:22
  • 1
    Gotcha. Well i posted an answer for the error & quit part of your question, but I need to do some digging to determine the rotation issue. I have a feeling it has to do with the combination of using the archiving feature with sequential log file numbering since it reads the directory to determine the next number. – JNYRanger Mar 11 '15 at 15:36

1 Answers1

1

This answer is for part B of your question, but I'll update it if I figure out part A based on some questions I posted as comments on your question and if I have some time I'll test it out myself as well.

You can't necessarily make the application error and quit based on opening the log file directly, however you can modify your configuration to cause NLog to throw exceptions, which you can handle as they bubble up or not handle and allow the application to crash. Additionally, you can get meta with NLog by having NLog log it's own processing. This may help you determine why the rogue file causes logging rotation to fail.

To cause nlog to throw exceptions add this attribute to the NLog root of your config:

<nlog throwExceptions="true">

NLog can log internal logs to either the console, or a log file:

<nlog internalLogFile="file.txt"> <!--File Logging -->
<nlog internalLogToConsole="true"> <!--Console Logging -->
<nlog internalLogToConsoleError="true"> <!--Log to stderr -->

You can also configure the verbosity of your logging as such:

<nlog internalLogLevel="Trace|Debug|Info|Warn|Error|Fatal" >

The above are all attributes that can be added to the root element <nlog> of your NLog config, which may be in it's own file nlog.config or a section of your web.config or appName.exe.config

It's very possible that you may have found a bug. I use NLog a lot for my application development with rotating file logs, but I have not yet experienced this. I did go through some of the code for the FileTarget in the repo and it does in fact read your files in the log path to determine sequential logging file names and dates. Having unexpected log files looks like it can potentially throw it off, but I would need to do some more digging to confirm if that's actually the issue. I did not see anything within the issue tracker on github for the nlog project referencing this issue.

Source: https://github.com/nlog/NLog/wiki/Configuration-file#troubleshooting-logging

JNYRanger
  • 6,829
  • 12
  • 53
  • 81