How do I configure a RollingFileAppender to roll by date and size with log4net?

前端 未结 4 1001
迷失自我
迷失自我 2020-12-15 17:42

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

相关标签:
4条回答
  • 2020-12-15 17:51

    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

    0 讨论(0)
  • 2020-12-15 17:59

    Note that is this case the

                <maxSizeRollBackups value="10"/>
    

    will be ignored.

    See this answer to a similar log4net question

    0 讨论(0)
  • 2020-12-15 18:09

    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

    0 讨论(0)
  • 2020-12-15 18:12

    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.

    0 讨论(0)
提交回复
热议问题