zoukankan      html  css  js  c++  java
  • RollingFileAppender bufferSize is not working? Here is an alternative

    RollingFileAppender bufferSize is not working? Here is an alternative

    If you have used log4net for your logging need, you may have come across the bufferSize property of the appender which will allow you to buffer x number of log entries and then it will flush. It is good for logging performance.

    Now you want to use RollingFileAppender and bufferSize is not working at all for you. You are searching internet and pulling your hairs why it is not working even though you would find bufferSize examples with different appenders working without any issue. Here is a sample log4net.config file

    <?xml version="1.0"?>
    <log4net>
        <appender name="fileAppender" type="log4net.Appender.RollingFileAppender" >
            <file type="log4net.Util.PatternString" value="Logfontconverter-%date{yyyyMMdd}.resx" />
            <bufferSize value="3" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="ALL"/>
        </root>
        <logger name="myLog">
            <level value="All"></level>
            <appender-ref ref="fileAppender" />
        </logger>
    </log4net>

    RollingFileAppender Class 没有一个叫做BufferSize的属性

    BufferingForwardingAppender Class 是有一个叫做BufferSize的属性

    Well, RollingFileAppender does not support bufferSize property and hence it will not work.

    Here is an alternative to buffer your logs and then flush it using RollingFileAppender. Instead of using RollingFileAppender, you need to use BufferingForwarder which in turn should use RollingFileAppender to write to log. Here is a working log4net.config file

    <?xml version="1.0"?>
    <log4net>
        <appender name="BufferingForwarder" type="log4net.Appender.BufferingForwardingAppender">
            <bufferSize value="5" />
            <lossy value="false" />
            <appender-ref ref="fileAppender" />
            <!-- or any additional appenders or other forwarders -->
        </appender>
        <appender name="fileAppender" type="log4net.Appender.RollingFileAppender" >
            <file type="log4net.Util.PatternString" value="Logfontconverter-%date{yyyyMMdd}.resx" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="ALL"/>
        </root>
        <logger name="myLog">
            <level value="All"></level>
            <appender-ref ref="BufferingForwarder" />
        </logger>
    </log4net>

    log4net 文件日志缓存(buffersize)

    这里也是同样使用buffer size的,组合BufferingForwardingAppender和RollingFileAppender的

    然后代码中这样写

    log4net.ILog log = log4net.LogManager.GetLogger("BufferingForwardingAppender");

    log.Info(“日志”);

    注意配置节点的顺序root→BufferingForwardingAppender→BufferingForwardingAppender→RollingLogFileAppender。

    root配置中不能出现<appender-ref ref="RollingLogFileAppender" />。

    再有bufferSize 的值不是大小,而是记录日志的次数,我配置的10,到第十一次的时候就会写入文件。

  • 相关阅读:
    Python魔法函数
    Python变量的本质与intern机制
    Python中IO概述
    自用官方文档索引(日常更新)
    关于 mysql json类型参数的查询过滤
    由于升级iOS版本导致证书丢失等一系列问题汇总
    Android系统获取手机型号和系统版本号,以及其他配置信息
    Android Debug证书过期的最佳解决方案
    关于编写高德导航Demo的手记(一)
    iOS6.0以上版本,关于NSDateFormatter的问题
  • 原文地址:https://www.cnblogs.com/chucklu/p/13261134.html
Copyright © 2011-2022 走看看