简体   繁体   中英

NLog log files not getting compressed but gets deleted

I am trying to zip and archive the old logs. I used the below target for this purpose.

<target name="PluginError" xsi:type="File" 
        layout="${longdate}${message}${exception:format=tostring}" 
        fileName="${basedir}/logs/Plugin/Error/${date:format=yyyy-MM-dd}.log" 
        archiveAboveSize="2000000" 
        archiveNumbering="Rolling" 
        maxArchiveFiles="10"  
        archiveFileName="${basedir}/logs/Plugin/Error/log.{#}.txt"             
        archiveEvery="Day"
        enableArchiveFileCompression="true"/>

But this deletes the old log files when count passes 3 instead of zipping them and archiving them. I am using NLog dll version 4.4.4090.0. What am I doing wrong here? Any help would be much appreciated.

The configuration you have specified will ensure:

  • maxArchiveFiles="10" - Max 10 files in archive-folder.
  • archiveEvery="Day" - Will move the current log-file to the archive-folder once a day.
  • archiveAboveSize="2000000" - Will move the current log-file to the archive-folder if it grows beyond 2 MByte.
  • archiveNumbering="Rolling" - Will ensure the lowest number (0) is the latest file.
  • archiveFileName="${basedir}/logs/Plugin/Error/log.{#}.txt" - Will rename the current log-file from log.0.txt to log.9.txt .
  • enableArchiveFileCompression="true" - Will compress each individual file using ZIP-format. Consider to change archiveFileName to have ZIP extension to match this decision.

If it behaves differently, then please try and change archiveEvery to Minute . If it continues to only have 3 files in the archive-folder, then please tell. Else I think some scheduled-task is cleaning up the archives-folder (or you have several NLog-file-targets pointing to the same folder?).

Maybe also check if you have any files in the Error-folder, that matches the wildcard log*.txt

i think there you are missing

enableArchiveFileCompression="true" 

you can use this nlog configuration its working fine http://nlog-project.org/2015/06/09/nlog-4-has-been-released.html

<?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" autoReload="true">
  <targets >
   <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" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="logfile">
    </logger>
  </rules>
</nlog>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM