参考网站如下,向原作者致敬。
http://www.cnblogs.com/manhoo/archive/2009/06/25/1510943.html
http://blog.csdn.net/fax5201314/article/details/8283538
http://hi.baidu.com/glaimei/item/09089f4df526a11fc11613a1
0)功能:能够输出五种日志,info, warn,debug, fatal, error. 不同类型的日志文件按日期命名。
1)在<configuration><configSections>后面插入一行:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
2)在</configSections>后面添加log4net节点,内容如下:
<log4net>
<root>
<!--
<level value="ALL" />
<appender-ref ref="rootFile" />
-->
</root>
<logger name="italklog">
<level value="ALL" />
<appender-ref ref="InfoLoging" />
<appender-ref ref="WarnLoging"/>
<appender-ref ref="DebugLoging" />
<appender-ref ref="FatalLoging" />
<appender-ref ref="ErrorLoging" />
</logger>
<!--INFO 日志-->
<appender name="InfoLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\log_info.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
<param name="Header" value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!-- WARN 日志-->
<appender name="WarnLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\log_warn.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
<param name="Header" value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<!-- DEBUG 日志-->
<appender name="DebugLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\log_debug.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
<param name="Header" value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<!-- FATAL 日志-->
<appender name="FatalLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\log_fatal.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
<param name="Header" value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<!--ERROR 日志-->
<appender name="ErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Log\log_error.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}%newline%message%n" />
<param name="Header" value="
[Header]
" />
<param name="Footer" value="[Footer]
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
</log4net>
3)在项目中添加log4net的Reference引用(下载地址)
4)在AssemInfo.cs中最后添加一行:
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
5)添加日志类LogHelper.cs,代码如下:
using System;
using System.IO;
public class LogHelper
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("italklog");
/// <summary>
/// 记录 调式信息日志
/// </summary>
/// <param name="strLog">调试日志的内容</param>
public static void Debug(string strLog)
{
if (log.IsDebugEnabled)
{
log.Debug(strLog);
}
}
/// <summary>
/// 记录 错误消息日志
/// </summary>
/// <param name="strLog">错误消息的内容</param>
public static void Error(string strLog)
{
if (log.IsErrorEnabled)
{
log.Error(strLog);
}
}
/// <summary>
/// 记录 致命信息日志
/// </summary>
/// <param name="strLog">致命消息的内容</param>
public static void Fatal(string strLog)
{
if (log.IsFatalEnabled)
{
log.Fatal(strLog);
}
}
/// <summary>
/// 记录 普通信息日志
/// </summary>
/// <param name="strLog">普通消息的内容</param>
public static void Info(string strLog)
{
if (log.IsInfoEnabled)
{
log.Info(strLog);
}
}
/// <summary>
/// 记录 警告信息日志
/// </summary>
/// <param name="strLog">警告消息的内容</param>
public static void Warn(string strLog)
{
if (log.IsWarnEnabled)
{
log.Warn(strLog);
}
}
}
6)代码使用使用实例:
LogHelper.Error("对不起,您不能访问该网页!");