Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

nlog

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 =)

like image 615
Carl Bergquist Avatar asked Oct 28 '09 09:10

Carl Bergquist


People also ask

How does NLog archive work?

So the NLog archiving feature does not work the way you want it to. It just places the last file of the month to the archive folder not all. This way you will get one file per day and the file will be transferred to the archive folder each day. The archive will keep the file for one year.

What can be configured with this NLog config file?

The following types can be configured: Targets - the destinations of a logevent, e.g. file, database, console. Layout - the layout e.g. json, csv, plain-text (default) Layout renderers - the template markers, e.g. ${message}, ${exception}, ${date}


2 Answers

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> 
like image 119
Brian Avatar answered Sep 24 '22 21:09

Brian


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

like image 37
the_joric Avatar answered Sep 24 '22 21:09

the_joric