A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/nlog/NLog/wiki/FileTarget-Archive-Examples below:

FileTarget Archive Examples · NLog/NLog Wiki · GitHub

NLog v4.5 improves dynamic archive mode, so it is easy to archive old files.

NLog v6.0 have introduced ArchiveSuffixFormat and obsoleted the following options:

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 produce the following files (and generate new file when 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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
 
    <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" />
    </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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
 
    <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" />
    </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:

<?xml version="1.0" ?>
<nlog xmlns="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>
        <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>
Archive Numbering Examples
        <target name="file" xsi:type="File"
            ...
            fileName="logfile.txt"
            archiveFileName="log.{####}.txt"
            archiveNumbering="Rolling"  />

Example of file names (newest files first):

        <target name="file" xsi:type="File"
            ...
            fileName="logfile.txt"
            archiveFileName="log.{####}.txt"
            archiveNumbering="Sequence"  />

Example of file names (newest files first):

        <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):

        <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):

Archive file in new folder

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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
 
    <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
One log file per application instance, remove old logs

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:

<?xml version="1.0" ?>
<nlog xmlns="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>
        <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>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4