写在前面
Log4Net是从Java的log4j移植过来的,功能也与log4j类似,可以把日志信息输出到文件、数据库、控制台、Windows 事件日志、远程系统日志服务等不同的介质或目标。
Log4Net配置选项丰富灵活,并且可在运行时动态更新配置并应用,允许同时推送到多个目标;虽然项目已经休眠了,但是该类库已久经考验,经典的设计永不过时。
获取类库
可通过NuGet获取类库,引入到项目中。
配置模板
新建log4net.xml文件,命名成log4net.config也可以,初始化时保持一致即可,配置内容如下:
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" LEVEL="INFO">
<!--防止多线程时不能写Log,官方说线程非安全-->
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Logs/" />
<param name="AppendToFile" value="true"/>
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyy-MM-dd.'info.log'" />
<param name="RollingStyle" value="Date"/>
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="## %d [%t] %-5p %x %m %n"/>
<!--<param name="ConversionPattern" value="%date [%thread] %-5level [%logger] %ndc - %message%newline" />-->
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender" LEVEL="ERROR">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Logs/" />
<param name="AppendToFile" value="true"/>
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyy-MM-dd.'error.log'" />
<param name="RollingStyle" value="Date"/>
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level [%logger] %ndc - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender" LEVEL="ERROR">
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Logs/" />
<param name="AppendToFile" value="true"/>
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyy-MM-dd.'debug.log'" />
<param name="RollingStyle" value="Date"/>
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level [%logger] %ndc - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ErrorFileAppender"/>
<appender-ref ref="DebugFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
将文件属性中的“复制到输出目录”配置为始终复制,这样配置文件会同步置执行或发布目录。
引用并初始化类库
static Program()
{
//加载log4net配置
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.xml"));
}
也可以在AssemblyInfo.cs中进行初始化配置
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.xml")]
工具类
public static class LogHelper
{
public static void Info(object msg, params object[] parameters)
{
var log = LogManager.GetLogger("log4netlogger");
log.Info(msg);
}
public static void Debug(string msg)
{
var log = LogManager.GetLogger("log4netlogger");
log.Debug(msg);
}
public static void Error(string msg)
{
var log = LogManager.GetLogger("log4netlogger");
log.Error(msg);
}
public static void FormatError(string format, params object[] args)
{
var log = LogManager.GetLogger("log4netlogger");
log.Error(string.Format(format, args));
}
public static void Error(Exception ex)
{
var log = LogManager.GetLogger("log4netlogger");
log.Error(ex.Message, ex);
}
public static void Error(object msg, Exception ex)
{
var log = LogManager.GetLogger("log4netlogger");
log.Error(msg, ex);
}
public static void Warn(object msg, Exception ex)
{
var log = LogManager.GetLogger("log4netlogger");
log.Warn(msg, ex);
}
public static void Log(string msg)
{
Info(msg);
}
public static void Log(Exception ex)
{
Error(ex);
}
}
调用示例
LogHelper.Info("事件类型出错,类型:" + _rv.EventType + ",参数:" + _rv.EventArgs);
LogHelper.Debug(string.Format("action_finish:{0}", TacticsName));
LogHelper.Error(string.Format("json序列化失败:{0}", json), ex);