在log4net的 config中设置:
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="BufferingForwardingAllAppender"/>
</root>
<appender name="BufferingForwardingAllAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="30" />
<appender-ref ref="NormalRollingFileAppender" />
<Fix value="0"/>
<!-- Set Fix flag to NONE -->
</appender>
<appender name="NormalRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<encoding type="System.Text.UTF8Encoding"/>
<file value="./log/" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="-1" />
<param name="StaticLogFileName" value="false" />
<param name="ImmediateFlush" value="true" />
<param name="RollingStyle" value="Composite" />
<param name="maximumFileSize" value="1GB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="DatePattern" value="yyyy-MM-dd."COMPASS.log"" />
<!--<param name="DatePattern" value="Log/Debug/yyyyMMdd.log"/>-->
<!--<param name="CountDirection" value="1"/>-->
<layout type="log4net.Layout.PatternLayout">
<!-- Print the date in ISO 8601 format -->
<!--<conversionPattern value="%date %-5level [%thread] %logger - %message%newline" />-->
<conversionPattern value="[%date] [%-5level] [线程:%-2t] [%-20logger{1}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
</log4net>
如代码所示,设置完毕后,log先进入buffer进行缓存,然后在第31条,输出 到文本中。
关于 Fix value=0 据说默认是all,不设置的话影响效率,本人拷贝代码,未验证