Tamás Somogyi
Tamás Somogyi

Reputation: 55

NLog is not deleting old files

I have the below config for NLog (4.7.11), which creates a new log file every day or when the file is overgrown. The problem: it is not deleting the old files at all so now the log folder is full of many weeks old log files. Can you please suggest what's wrong and how to get it work?

<target name="appLogFile" type="File"
 fileName="d:\my_logs\nlog_test-${date:format=yyyy-MM-dd}.log"
 archiveFileName="d:\my_logs\nlog_test-{#}.log" 
 archiveAboveSize="104857600" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd"
 maxArchiveFiles="2" maxArchiveDays="2"
 openFileCacheTimeout="30" concurrentWrites="True" keepFileOpen="True" createDirs="True"
 layout="${date:format=yyy-MM-dd HH\:mm\:ss.fff K} ${message}" />

Upvotes: 0

Views: 832

Answers (2)

Rolf Kristensen
Rolf Kristensen

Reputation: 19922

NLog 4.5 (and newer) makes it easy to do file-archive with dynamic-layout:

<target name="appLogFile" type="File"
 fileName="d:\my_logs\nlog_test-${date:format=yyyy-MM-dd}.log"
 archiveAboveSize="104857600"
 maxArchiveFiles="2" maxArchiveDays="2"
 openFileCacheTimeout="30" concurrentWrites="True" keepFileOpen="True"
 layout="${date:format=yyy-MM-dd HH\:mm\:ss.fff K} ${message}" />

By removing archiveFileName + archiveNumbering + archiveDateFormat.

See also: https://github.com/NLog/NLog/wiki/File-target#archive-old-log-files

See also: https://github.com/NLog/NLog/wiki/File-target#dynamic-vs-static-archive-logic

Upvotes: 1

Jinu
Jinu

Reputation: 78

The issue looks like, you are logging into different files each day because the file names are created with the current date. On the next day, logging is done to the new file. When the replacing is not happening to the current file then the archive is not created until the maximum size is reached.

Please try the bellow configuration,

<target name="appLogFile" type="File"
 fileName="d:\my_logs\nlog_test.log"
 archiveFileName="d:\my_logs\archives\nlog_test-{#}.log" 
 archiveAboveSize="104857600" archiveNumbering="DateAndSequence" archiveDateFormat="yyyy-MM-dd"
 maxArchiveFiles="2" maxArchiveDays="2"
 openFileCacheTimeout="30" concurrentWrites="True" keepFileOpen="True" createDirs="True"
 layout="${date:format=yyy-MM-dd HH\:mm\:ss.fff K} ${message}" />

Upvotes: 1

Related Questions