- Notifications
You must be signed in to change notification settings - Fork1.4k
FileTarget Archive Examples
NLog v4.5 improves dynamic archive mode, so it iseasy to archive old files.
NLog v6.0 have introducedArchiveSuffixFormat and obsoleted the following options:
ArchiveDateFormatArchiveNumberingArchiveFileKindArchiveOldFileOnStartupAboveSizeEnableArchiveFileCompression
These examples shows thearchive options for the static-archive-mode. One shouldNot 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 producethe following files (and generate new file when file reaches 10KB):
- logs/logfile.txt // the current log being written to
- archives/log.000000.txt // The oldest log file
- archives/log.000001.txt
- archives/log.000002.txt
- etc.
<?xml version="1.0" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targets> <targetname="file"xsi:type="File"layout="${longdate} ${logger} ${message}"fileName="${basedir}/logs/logfile.txt"archiveFileName="${basedir}/archives/log.{#####}.txt"archiveAboveSize="10240"archiveNumbering="Sequence" /> </targets> <rules> <loggername="*"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:
- logs/logfile.txt // todays log being written to
- archives/log.0.txt // previous log file from yesterday
- archives/log.1.txt
- archives/log.2.txt
- archives/log.3.txt
- archives/log.4.txt
- archives/log.5.txt
- archives/log.6.txt // Oldest file defined by maxArchiveFiles=7
<?xml version="1.0" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targets> <targetname="file"xsi:type="File"layout="${longdate} ${logger} ${message}"fileName="${basedir}/logs/logfile.txt"archiveFileName="${basedir}/archives/log.{#}.txt"archiveEvery="Day"archiveNumbering="Rolling"maxArchiveFiles="7" /> </targets> <rules> <loggername="*"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 setarchiveEvery="Tuesday". Possible values forarchiveEvery can be found above. This will result inthe 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" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targets> <targetname="file"xsi:type="File"layout="${longdate} ${logger} ${message}"fileName="${basedir}/logs/logfile.txt"archiveFileName="${basedir}/archives/logfile.{#}.txt"archiveEvery="Tuesday"maxArchiveFiles="7" /> </targets> <rules> <loggername="*"minlevel="Debug"writeTo="file" /> </rules></nlog>
<targetname="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
<targetname="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
<targetname="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
<targetname="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 alsoDo not mix dynamic-filename with static archive logic
<?xml version="1.0" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targets> <targetname="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> <loggername="*"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.txtThe 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 thearchiveFileName contains a placeholder,archiveDateFormat has the same datetime format as in thename property, andarchiveNumbering andarchiveEvery 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" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> <targets> <targetname="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> <loggername="*"minlevel="Debug"writeTo="file" /> </rules></nlog>
-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json