32

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. But all the logs for this day should be placed in Log.log for tailing and such.

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 one # in the archiveFileName, which I rather not. So if you got a solution for that also I would be twice as thankful =)

2
  • 1
    Sorry to return to a really old thread...but did you ever find a solution to this? Feb 28, 2015 at 16:42
  • @AndrewJones Nope. I've moved on to the elastic / logstash and kibana stack. Mar 1, 2015 at 17:05

3 Answers 3

59

Probably too late to help you, but I believe all you need to do is include the date in the file name using the date layout renderer with the proper date format. By including the date, you don't need to specify the archive features. When the date changes, a new file will be automatically created.

<?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/${date:format=yyyy-MM-dd}.log"
                  layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
                  keepFileOpen="true"
                />
    </targets>
  <rules>
      <logger name="*" minlevel="Trace" writeTo="file1" />
  </rules>
</nlog>
3
  • Not to late :) I forgot to mention that I still want the active log to be named Log.log for tailing and such. So this solution is good, but not perfect :) Jun 8, 2010 at 7:56
  • 2
    Also, this solution disables the possibility to auto-erase old archive log files...
    – Verthosa
    Jan 17, 2018 at 14:04
  • the "date layout renderer" link is broken, can u fix it?
    – Markus
    Jul 7, 2020 at 13:34
8

Just in case if somebody still needs a solution -- requested feature has been added to NLog recently: https://github.com/NLog/NLog/pull/241, but it is still not available by Nuget

0
-3

Maybe this is what you need, Daily folder with the file Log.log in it

<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
2
  • What about this config makes it daily?
    – Mike Cole
    Jan 8, 2020 at 17:24
  • @Mike Cole Nothing
    – JSON
    Nov 30, 2020 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.