I’ve a .net framework application which writes tons of logs. My log files rolling hourly, and generating by NLog. These logs are critical for last 15 days. Older than 15 days of logs are inaccurate/useless. Due to the frequency of logging structure, my log folder is going to be huge. Also I can’t access the production environment server, so I want to do a robust and reliable solution for deleting older log files. Currently I use the config below;
<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="LogDir" value="Log"/>
<variable name="LogDailyFormat" value="${date:format=yyyyMMdd}"/>
<variable name="LogHourlyFormat" value="${date:format=yyyyMMdd-HH}"/>
<variable name="InfoFileName" value="Info"/>
<variable name="ErrorFileName" value="Error"/>
<variable name="HeartbeatFileName" value="HeartBeat"/>
<variable name="ArchiveMaxDays" value="15"/>
<variable name="ArchiveEvery" value="Day"/>
<targets>
<target name="InfoLogger" xsi:type="File"
concurrentWrites="true"
keepFileOpen="true"
fileName="${LogDir}/${InfoFileName}_${LogHourlyFormat}.log"
encoding="utf-8"
archiveEvery="{ArchiveEvery}"
maxArchiveDays="{ArchiveMaxDays}" />
<target name="ErrorLogger" xsi:type="File"
concurrentWrites="true"
keepFileOpen="true"
fileName="${LogDir}/${ErrorFileName}_${LogHourlyFormat}.log"
encoding="utf-8"
archiveEvery="{ArchiveEvery}"
maxArchiveDays="{ArchiveMaxDays}" />
<target name="HeartBeatLogger" xsi:type="File"
fileName="${LogDir}/${HeartbeatFileName}_${LogDailyFormat}.log"
encoding="utf-8"
archiveEvery="{ArchiveEvery}"
maxArchiveDays="{ArchiveMaxDays}" />
</targets>
<rules>
<logger name="HeartBeat" writeTo="HeartBeatLogger" final="true" />
<logger name="*" minlevel="Trace" maxlevel="Info" writeTo="InfoLogger" final="true"/>
<logger name="*" minlevel="Warn" maxlevel="Fatal" writeTo="ErrorLogger" final="true"/>
</rules>
</nlog>
As you see the config, I can delete older log files with NLog archiving feature but I’m not sure which is better, should I use a scheduled task to clear my old logs or NLog? Which will be more robust and reliable solution? I’m open to any advice.
Thanks in advance.