Nlog: Archiving of logs whose file name depends on MDC

Created on 2 Nov 2016  路  4Comments  路  Source: NLog/NLog

Type:

  • Bug

NLog version: 4.3.10

Platform: .Net 4.6.1

Current NLog config (xml or C#, if relevant)

<nlog>
    <targets>
      <target
        name="f1"
        type="File"
        layout="${longdate} | ${level} | ${message}"
        fileName="${basedir}/Log/log_${mdc:item=clientId}.txt"
        archiveEvery="Minute"
        archiveFileName="${basedir}/Log/archive/log_${mdc:item=clientId}.{#}.txt"
        archiveNumbering="DateAndSequence"
        archiveAboveSize="1000"
        archiveDateFormat="yyyy-MM-dd" />
    </targets>
    <rules>
      <logger name="PerClientLogger" minLevel="Trace" writeTo="f1" final="true" />
    </rules>
  </nlog>

  • What is the current result?
    Let's say that I use PerClientLogger and MappedDiagnosticsContext.Set("clientId", "First") with the config above and fill the log log_First.txt to 999 bytes. If I then switch to MappedDiagnosticsContext.Set("clientId", "Second") and log another message, log_First.txt is archived as log_Second.2016-11-02.0.txt (notice the wrong name!) and log_Second.txt is created. The same thing happens when log_First.txt is older than a minute and I log a message that should go to log_Second.txt.

  • What is the expected result?
    Writing to log_Second.txt should not trigger the archiving of log_First.txt at all, let alone use a wrong name for it.

file-target question

Most helpful comment

PR #1993 will make it possible to perform correct archive cleanup, as the rendering of the archive-cleanup-filemask is delayed until having an actual LogEvent (with all details).

All 4 comments

archived as log_Second.2016-11-02.0.txt (notice the wrong name!

Well that is bit difficult, as we don't have the previous value of ${mdc:item=clientId} anymore.

IMO this is expected, as it could be also:

    fileName="${basedir}/Log/log_${mdc:item=myfilename}.txt"
    archiveEvery="Minute"
    archiveFileName="${basedir}/Log/archive/log_${mdc:item=myarchivename}.{#}.txt"

Writing to log_Second.txt should not trigger the archiving of log_First.txt at all, let alone use a wrong name for it.

There is only 1 filetarget here and so it's sharing the properties. For this case you need a second FileTarget.

So in other words, it is impossible to write to logs with arbitrary names determined at runtime while archiving each file separately?

write to logs with arbitrary names determined at runtime

that's possible

archiving each file separately?

Currently not

PR #1993 will make it possible to perform correct archive cleanup, as the rendering of the archive-cleanup-filemask is delayed until having an actual LogEvent (with all details).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imanushin picture imanushin  路  3Comments

smeegoan picture smeegoan  路  3Comments

sszost picture sszost  路  3Comments

carkov1990 picture carkov1990  路  3Comments

Sam13 picture Sam13  路  3Comments