-
Notifications
You must be signed in to change notification settings - Fork 1.4k
FileTarget Archive Examples
NLog ver. 4.5 improves dynamic archive mode, so it is easy to archive old files.
These examples shows the archive options for the static-archive-mode. One should Not mix dynamic with static-archive-mode.
Log files can be automatically archived by moving them to another location after reaching certain size. The following configuration will create logs/logfile.txt which will be archived to archives/log.000000.txt', archives/log.000001.txt', archives/log.000002.txt' and so on once the main log file reaches 10KB.
<?xml version="1.0" ?>
<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"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/logs/logfile.txt"
archiveFileName="${basedir}/archives/log.{#####}.txt"
archiveAboveSize="10240"
archiveNumbering="Sequence"
concurrentWrites="true"
keepFileOpen="false"
encoding="iso-8859-2" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
Log files can also be automatically archived based on time. This configuration will archive a file at the beginning of each day and will use rolling file naming, so log file from the previous day can always be found in archives//log.0.txt, log from two days ago is in archives//log.1.txt and so on. This configuration will keep at most 7 archive files, so logs older than one week will be automatically deleted.
<?xml version="1.0" ?>
<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"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/logs/logfile.txt"
archiveFileName="${basedir}/archives/log.{#}.txt"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7"
concurrentWrites="true"
keepFileOpen="false"
encoding="iso-8859-2" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
You can specify different archival time periods. For example, if you wanted to archive once a week on Tuesdays,
you would set archiveEvery="Tuesday"
. Possible values for archiveEvery
can be found above. This will result in
the following files being created:
- logfile.txt // the current log being written to
- logfile.20170307.txt
- logfile.20170314.txt
- logfile.20170321.txt
- etc.
<?xml version="1.0" ?>
<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"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/logs/logfile.txt"
archiveFileName="${basedir}/archives/logfile.{#}.txt"
archiveEvery="Tuesday"
maxArchiveFiles="7" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
<target name="file" xsi:type="File"
...
fileName="logfile.txt"
archiveFileName="log.{####}.txt"
archiveNumbering="Rolling" />
Example of file names (newest files first):
- logfile.txt
- log.0000.txt
- log.0001.txt
- log.0002.txt
<target name="file" xsi:type="File"
...
fileName="logfile.txt"
archiveFileName="log.{####}.txt"
archiveNumbering="Sequence" />
Example of file names (newest files first):
- logfile.txt
- log.0002.txt
- log.0001.txt
- log.0000.txt
<target name="file" xsi:type="File"
...
fileName="logfile.txt"
archiveFileName="log.{#}.txt"
archiveNumbering="Date"
archiveEvery="Day"
archiveDateFormat="yyyyMMdd"
/>
Example of file names (newest files first):
- logfile.txt
- log.20150731.txt
- log.20150730.txt
<target name="file" xsi:type="File"
...
fileName="logfile.txt"
archiveFileName="log.{#}.txt"
archiveNumbering="DateAndSequence"
archiveAboveSize="1000"
archiveDateFormat="yyyyMMdd"
/>
Example of file names (newest files first):
- logfile.txt
- log.20150730.3.txt
- log.20150730.2.txt
- log.20150730.1.txt
NLog also has support for writing to a static fileName-Layout, and then move the file to archive-location and re-create new fresh file. But it only works when not including dynamic layout (Ex. ${date}
) in the fileName-Layout or archiveFileName-Layout. See also Do not mix dynamic-filename with static archive logic
<?xml version="1.0" ?>
<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"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/logs/AppLog.txt"
archiveFileName="${basedir}/archives/AppLog.{#}.txt"
archiveEvery="Day"
maxArchiveFiles="4"
archiveAboveSize="10240" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
It will generate the following filenames (newest first):
* AppLog.txt // the current log being written to
* AppLog.20170321.txt
* AppLog.20170314.txt
* AppLog.20170307.txt
The following configuration will create a dedicated log file for each start of your application. Multiple instances can run in parallel and write to their respective log file. By adding a timestamp to the filename, each filename is unique (up to the second). Up to ten log files (active + nine archive) are kept. The removal of old logs works, when the archiveFileName
contains a placeholder, archiveDateFormat
has the same datetime format as in the name
property, and archiveNumbering
and archiveEvery
are enabled. The $(cached:...)
directive prevents that a new log file name is generated for every log entry. Log files will be named:
- 2017-11-05 08_00_00.log
- 2017-11-05 08_00_01.log
- 2017-11-05 12_35_04.log
- 2017-11-06 09_54_32.log
- ...
<?xml version="1.0" ?>
<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"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log"
archiveFileName="${basedir}/{#}.log"
archiveDateFormat="yyyy-MM-dd HH_mm_ss"
archiveNumbering="Date"
archiveEvery="Year"
maxArchiveFiles="9" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json