26

I am configure log4net to use a composite RollingFileAppender so that the current file is always named logfile.log and all subsequent files are named logfile-YYYY.MM.dd.seq.log where seq is the sequence number if a log exceeds a certain size within a single day. Unfortunately, I have had very little success in configuring such a setup.

Edit:

My current configuration is pasted below. It has been updated based on several answers which gets me close enough for my needs. This generates files of the format: logfile_YYYY.MM.dd.log.seq

<log4net>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingFileAppender" />
    </root>

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="logs\\logfile"/>
        <staticLogFileName value="false"/>
        <appendToFile value="true"/>
        <rollingStyle value="Composite"/>
        <datePattern value="_yyyy.MM.dd&quot;.log&quot;"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="75KB"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="FATAL" />
        </filter>
    </appender>

</log4net>

One interesting note, setting

<staticLogFileName value="false"/>

to true causes the logger to not write any files.

Askolein
  • 3,250
  • 3
  • 28
  • 40
Ryan Taylor
  • 8,740
  • 15
  • 65
  • 98

4 Answers4

16

We use the following (in Log4J):

<appender name="roller" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="Applog.log"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[slf5s.start]%d{DATE}[slf5s.DATE]%n%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n"/>
    </layout>
</appender>

This gives us Applog.log.yyyy-MM-dd files

paul
  • 13,312
  • 23
  • 81
  • 144
  • 1
    Per the log4j documentation: DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender. – Corey Cole Sep 11 '15 at 15:46
4

According to log4net RollingFileAppender source code:

protected string GetNextOutputFileName(string fileName)
{
    if (!m_staticLogFileName) 
    {
        fileName = fileName.Trim();

        if (m_rollDate)
        {
            fileName = fileName + m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo);
        }

        if (m_countDirection >= 0) 
        {
            fileName = fileName + '.' + m_curSizeRollBackups;
        }
    }

    return fileName;
}

So I'm guessing it's not possible to generate a log file with the name you need. I think it's something like logfileYYYY-MM-dd.n.log or similar.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Leandro López
  • 2,185
  • 1
  • 15
  • 17
  • @Ryan, Leandro: It also appears there's no simple way to override this behavior. `GetNextOutputFileName` isn't virtual, and other methods that call it reference private variables, so you can't simply copy their code, and replace the call. I think the best you can do is have a unique file name each time, using something like the date pattern option you ended up using. – Merlyn Morgan-Graham Feb 22 '11 at 22:45
2

Try set this property to true:

preserveLogFileNameExtension value="true"

I believe this trick will help you! However, preserveLogFileNameExtension property needs the latest version of log4net, you can find it here: logging.apache.org/log4net/download.html

Junrui
  • 21
  • 1
1

Note that is this case the

            <maxSizeRollBackups value="10"/>

will be ignored.

See this answer to a similar log4net question

Community
  • 1
  • 1
Charley Rathkopf
  • 4,720
  • 7
  • 38
  • 57