一直使用Log4net,进行日志记录。今天把实用的帮助类,在博客园进行一下公布
首先,添加一个log4net配置文件
<?xml version="1.0"?>
<log4net debug="false" >
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="Logs/"/>
<datePattern value="yyyy-MM/yyyy-MM-dd.LOG"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[时刻]:%d%n[类别]:%p%n[消息]:%m%n[线程]:%t%n[文件]:%F%n[对象]:%c%n[行号]:%L%n[消耗]:%r毫秒%n%n" />
</layout>
</appender>
<root>
<level value="OFF"/>
</root>
<logger additivity="false" name="lance" >
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</logger>
</log4net>
很全,基本上要看的也就这几个了
然后用一个单例,来加载Log帮助类,程序中均调用此帮助类进行日志记录功能
public class LogHelper
{
private static LogHelper logHelper = null;
public ILog Log
{
get;
set;
}
private LogHelper()
{
Log = log4net.LogManager.GetLogger("lance");
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Pages\\log4net.config";
System.IO.FileInfo file = new System.IO.FileInfo(path);
log4net.Config.XmlConfigurator.Configure(file);
} public static LogHelper GetInstance()
{
if (logHelper == null)
{
logHelper = new LogHelper();
}
return logHelper;
}
}
接下来就是愉快的调用了
ILog log = LogHelper.GetInstance().Log;
log.Info("这里写需要记录的东西");
Log4net 具体配置明细 推荐一个博客:http://blog.csdn.net/pfe_nova/article/details/12225349