Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...