Log4net 扩展RollingFileAppender

扩展RollingFileAppender实现Error级别的日志发送预警

  public class CRollingFileAppender : RollingFileAppender
    {
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            base.Append(loggingEvent);
            try
            {
                Level level = loggingEvent.Level;
                if (level < Level.Error)
                    return;
                SendAlarm send = new SendAlarm(Send);
                send.BeginInvoke(loggingEvent.MessageObject + "", level.Name, null, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public delegate void SendAlarm(string errorMessage, string loggingLevel);
        public void Send(string errorMessage, string loggingLevel)
        {
        }
    }
}    

配置:

    <log4net>
        <appender name="RollingLogFileAppender" type="xxx.xx.CRollingFileAppender, log4net">
            <param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
            <param name="File" value="Log\" />
            <param name="AppendToFile" value="true" />
            <param name="RollingStyle" value="Composite" />
            <param name="StaticLogFileName" value="false" />
            <param name="MaxSizeRollBackups" value="-1" />
            <param name="maximumFileSize" value="2048KB" />
            <param name="DatePattern" value="&quot;trace_&quot;yyyyMMddHH&quot;.log&quot;" />
            <layout type="log4net.Layout.PatternLayout">
                <param name="ConversionPattern" value="%d [%t] %-5p %m %n" />
            </layout>
        </appender>

        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingLogFileAppender" />
        </root> 

    </log4net>

使用:

public readonly static ILog NEWS_LOG = LogManager.GetLogger("RollingLogFileAppender");
NEWS_LOG.Error("测试");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值