Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress log files with NLog

I am using NLog in one of my projects and I am trying to have the output of the files to be compressed. I tried to use the compress file attribute, but when I look at the files, they are not compressed.

Could you please tell me what I might be doing wrong?

This is my config:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>  
        <target name="file" xsi:type="File" fileName="C:\Workspaces\log.xml"
                layout="${message}" keepFileOpen="true"
                archiveFileName = "C:\Workspaces\archived\log.{#####}.xml"
                archiveAboveSize = "1048576" archiveNumbering = "Sequence"
                fileAttributes="Compressed" concurrentWrites =  "true"/>
    </targets>

    <rules>
        <logger name ="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog> 
like image 697
SysCoder Avatar asked May 04 '12 16:05

SysCoder


1 Answers

With NLog 4, you can compress to zip files (.NET 4.5+). See the NLog 4.0 release post

Use enableArchiveFileCompression as follows:

<target name="file" xsi:type="File"
      layout="${longdate} ${logger} ${message}" 
      fileName="${basedir}/logs/logfile.txt" 
      archiveFileName="${basedir}/archives/log.{#}.txt"
      archiveEvery="Day"
      archiveNumbering="Rolling"
      maxArchiveFiles="7"
    enableArchiveFileCompression="true" />
like image 197
Julian Avatar answered Oct 11 '22 03:10

Julian