Is there any similar kind of configure to rolingStyle="once" of log4net is available for NLog?

Viewed 125

I am using log4net currently and on every hour log file archive is being performed. Now I am changing log4net to NLog

Is there similar setting available in NLog Like rolingStyle="once"? which was configure setting available in log4net.

For example earlier while using log4net the files created were used to be like:

LogFile.log
LogFile.log.1 <-last archive file

Following is configuration I used in log4net and I want to use the exact configure settings so that archived file naming should remains as it was in log4net:

<appender name="Work" type="RMM.Common.Logger.LogFileRollingAppender, Common">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <dateTimeStrategy type="log4net.Appender.RollingFileAppender+UniversalDateTime" />
    <file type="log4net.Util.PatternString" value="%property{EdgeAPILogPath}\WebAPI_Work.log" />
    <param name="AppendToFile" value="true"/>
    <rollingStyle value="Once" />
    <rollingStyle value="Composite" />
    <datePattern value=".yyyyMMdd-HH" />
    <maxSizeRollBackups value="300" />
    <maximumFileSize value="20MB" />
    <Encoding value="UTF-8" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%utcdate{yyyy/MM/dd HH:mm:ss,fff}&#9;[%-5p]&#9;[%3t]&#9;%m%n" />
    </layout>
    <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="FATAL" />
    </filter>
</appender>
2 Answers

You can:

  • Use fileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log" as a way to ensure one log file per application instance.
  • Use archiveFileName="${archiveLogDirectory}/LogFile.log.{####}" to append numbers at the end (feel free to add or remove # as required, depending on your maxArchiveFiles).
  • Use archiveNumbering="Sequence" to achieve the order you want (higher numbers = newer logs).

Source: this piece of documentation and some personal experience.

Hopefully this basic example will help you getting closer to your final target:

<?xml version="1.0" encoding="utf-8" ?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Error"
      internalLogFile="./internal-nlog.txt"
      throwExceptions="true">

    <variable name="logDirectory" value="./logs"/>
    <variable name="archiveLogDirectory" value="./logs/archive"/>

    <targets>
        <target name="errors"
            xsi:type="File"
            fileName="${logDirectory}/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.log"
            archiveFileName="${archiveLogDirectory}/LogFile.log.{#}"
            maxArchiveFiles="9"
            archiveEvery="Hour"
            archiveNumbering="Sequence"
        />
    </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="errors"/>
  </rules>
</nlog>

Not an expert on log4net, but it sounds like that rolingStyle="once" is the same as NLog FileTarget ArchiveOldFileOnStartup. So maybe something like this:

<nlog>
<variable name="EdgeAPILogPath" layout="${basedir}" />

<targets>
   <target xsi:type="file" name="work"
           fileName="${EdgeAPILogPath}/WebAPI_Work.log" 
           encoding="utf-8"
           archiveNumbering="DateAndSequence"
           archiveFileName="${EdgeAPILogPath}/WebAPI_Work.{#}.log"
           archiveDateFormat="yyyyMMdd-HH"
           archiveEvery="Hour"
           archiveAboveSize="20000000"
           archiveOldFileOnStartup="true"
           maxArchiveFiles="300" />
</targets>

<rules>
   <logger name="*" minLevel="Debug" writeTo="work" />
</rules>
</nlog>

See also: https://github.com/NLog/NLog/wiki/FileTarget-Archive-Examples

Related