How to make Nlog archive a file with the date when the logging took place.

Posted by Carl Bergquist on Stack Overflow See other posts from Stack Overflow or by Carl Bergquist
Published on 2009-10-28T09:01:13Z Indexed on 2010/06/08 0:02 UTC
Read the original article Hit count: 436

Filed under:
|

Hi,

We are using Nlog as our logging framework and I cannot find a way to archive files the way I want. I would like to have the date of when the logging took place in the logging file name.
Ex All logging that happend from 2009-10-01 00:00 -> 2009-10-01:23:59 should be placed in Log.2009-10-01.log

The current NLog.config that I use looks like this.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <extensions>
    <add assembly="My.Awesome.LoggingExentions"/>
  </extensions>
    <targets>
    	<target name="file1" xsi:type="File"
    		  fileName="${basedir}/Logs/Log.log"
    		  layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
    		  archiveEvery="Day"
    		  archiveFileName="${basedir}/Logs/Log${shortdate}-{#}.log"
    		  archiveNumbering="Sequence"
    		  maxArchiveFiles="99999"
    		  keepFileOpen="true"
    		/>
    </targets>
  <rules>
      <logger name="*" minlevel="Trace" writeTo="file1" />
  </rules>
</nlog>

This however sets the date in the logfile to the date when the new logfile is created. Which cause frustration when you want to read logs later.

It also seems like I have to have atleast on # in the archiveFileName, which I rather not. So if you got a solution for that also I would be twice as thankful =)

© Stack Overflow or respective owner

Related posts about c#

Related posts about nlog