1:建log4net.config配置文件
2:创建一个Logger静态类写日志
一:在App或者Web根目录创建一个log4net.config文件,完整内容如下:
一定要注意这就是完整的配置文件内容,有些文章里面说要把内容拷在<configuration>节点下,结果不能正常写日志
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="Logs\err.log" />
<encoding value="utf-8"/>
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="Logs\info.log" />
<encoding value="utf-8"/>
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="Logs\debug.log" />
<encoding value="utf-8"/>
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="Logs\perf.log" />
<encoding value="utf-8"/>
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="errorAppender" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
</root>
<logger name="Performance" additivity="false">
<level value="ALL" />
<appender-ref ref="perfAppender" />
</logger>
</log4net>
这个配置方法定义了几个过滤器,把不同级别的日志写在不同名称的文件中,也可以修改配置写到一个日志文件中。
二:创建日志静态类
/// <summary>
/// 框架日志类
/// </summary>
public class WRLogger
{
static ILog _logger = null;
/// <summary>
/// 默认日志类
/// </summary>
static ILog logger
{
get
{
if (_logger == null)
{
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
_logger = LogManager.GetLogger("Performance");
}
return _logger;
}
}
/// <summary>
/// 记录信息日志
/// </summary>
/// <param name="msg"></param>
public static void Info(string msg)
{
logger.Info(msg);
}
/// <summary>
/// 记录调试日志,主要用于SQL或一些方法调用
/// </summary>
/// <param name="msg"></param>
public static void Debug(string msg) {
logger.Debug(msg);
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="msg"></param>
public static void Error(string msg) {
logger.Error(msg);
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="ex"></param>
public static void Error(Exception ex)
{
logger.Error(ex);
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="ex"></param>
/// <param name="msg"></param>
public static void Error(Exception ex,string msg)
{
logger.Error( msg+" "+ex.Message, ex);
}
}
其中:
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
#这句话特别关键,载入配置文件,这种方式载入, 1:则不需要
#在“AssemblyInfo.cs”中写配置
#2:也不需要在app.config或者web.config中写任何关于log4net的配置信息这样就可以直接使用了
以上内容转自网络