扩展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=""trace_"yyyyMMddHH".log"" />
<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("测试");