Maximum Filesize of LogFileAppender in Log4Net

前端 未结 2 1784
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 15:15

I am using Log4net for a while now and it\'s an amazing logging framework, especially when hooked into Castle.Windsor. However...

I usually use the rolling file app

相关标签:
2条回答
  • 2020-12-09 15:26

    The LogFileAppender does not support limiting the output file size (at least in the references I can find). To limit the file size, use a RollingFileAppender and roll on Size and set the file size limit.

    To limit the number of roll over files use the MaxSizeRollBackups attribute

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

    The FileAppender class does not have the MaxFileSize/MaximumFileSize properties. You only get those if you use a RollingFileAppender. Here's an example that will limit your file to a fixed maximum size, with no backups (set maxSizeRollBackups to 0). Note that when the file reaches its max size, it truncates (basically deletes all existing logging and starts over):

    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="0" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    
    0 讨论(0)
提交回复
热议问题