<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!--添加自定义节点:log4net type:解析类名,程序集名(log4net.dll)-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections> <log4net>
<!--定义输出到文件中-->
<appender name="Log4Net_INFO" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="C:/log4net/"/>
<!--是否追加到文件,默认为true,通常无需设置-->
<appendToFile value="true"/>
<RollingStyle value="Date"/>
<!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
<DatePattern value="INFO_yyyyMMdd".log"" />
<!--日志文件名是否为静态-->
<StaticLogFileName value="false"/>
<!--多线程时采用最小锁定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--布局(向用户显示最后经过格式化的输出信息)-->
<layout type="log4net.Layout.PatternLayout">
<!--
%m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息
%n(new line):换行
%d(datetime):输出当前语句运行的时刻
%r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
%t(thread id):当前语句所在的线程ID
%p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
%c(class):当前日志对象的名称,例如:
%L:输出语句所在的行号
%F:输出语句所在的文件名
%-数字:表示该项的最小长度,如果不够,则用空格填充
-->
<Header value="[Header] "/>
<Footer value="[Footer] "/>
<!--正文-->
<ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" />
</layout>
</appender> <appender name="Log4Net_ERROR" type="log4net.Appender.RollingFileAppender">
<file value="C:/log4net/"/>
<appendToFile value="true"/>
<RollingStyle value="Date"/>
<DatePattern value="ERROR_yyyyMMdd".log"" />
<StaticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<Header value="[Header] "/>
<Footer value="[Footer] "/>
<!--正文-->
<ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" />
</layout>
</appender> <root>
<level value="ERROR"/>
<appender-ref ref="Log4Net_ERROR" /> <level value="INFO"/>
<appender-ref ref="Log4Net_INFO" />
</root> </log4net> </configuration>
配置文件
using log4net;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic; //指定log4net使用的config文件来读取配置信息
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"LogConfig\Log4Net.config", Watch = true)]
namespace Project.Log4.Net.LogConfig
{
/// <summary>
/// 日志帮助类
/// </summary>
public class LogHelper
{
private static readonly ConcurrentDictionary<Type, ILog> _loggers = new ConcurrentDictionary<Type, ILog>(); /// <summary>
/// 获取记录器
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
private static ILog GetLogger(Type source)
{
if (_loggers.ContainsKey(source))
{
return _loggers[source];
}
else
{
ILog logger = LogManager.GetLogger(source);
_loggers.TryAdd(source, logger);
return logger;
}
} /* Log a message object */ /// <summary>
/// 调试信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Debug(object source, string message)
{
Debug(source.GetType(), message);
} /// <summary>
/// 调试信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="ps"></param>
public static void Debug(object source, string message, params object[] ps)
{
Debug(source.GetType(), string.Format(message, ps));
} /// <summary>
/// 调试信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Debug(Type source, string message)
{
ILog logger = GetLogger(source);
if (logger.IsDebugEnabled)
logger.Debug(message);
} /// <summary>
/// 关键信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Info(object source, object message)
{
Info(source.GetType(), message);
} /// <summary>
/// 关键信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Info(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsInfoEnabled)
logger.Info(message);
} /// <summary>
/// 警告信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Warn(object source, object message)
{
Warn(source.GetType(), message);
} /// <summary>
/// 警告信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Warn(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsWarnEnabled)
logger.Warn(message);
} /// <summary>
/// 错误信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Error(object source, object message)
{
Error(source.GetType(), message);
} /// <summary>
/// 错误信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Error(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsErrorEnabled)
logger.Error(message);
} /// <summary>
/// 失败信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Fatal(object source, object message)
{
Fatal(source.GetType(), message);
} /// <summary>
/// 失败信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
public static void Fatal(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsFatalEnabled)
logger.Fatal(message);
} /* Log a message object and exception */ /// <summary>
/// 调试信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Debug(object source, object message, Exception exception)
{
Debug(source.GetType(), message, exception);
} /// <summary>
/// 调试信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Debug(Type source, object message, Exception exception)
{
GetLogger(source).Debug(message, exception);
} /// <summary>
/// 关键信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Info(object source, object message, Exception exception)
{
Info(source.GetType(), message, exception);
} /// <summary>
/// 关键信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Info(Type source, object message, Exception exception)
{
GetLogger(source).Info(message, exception);
} /// <summary>
/// 警告信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Warn(object source, object message, Exception exception)
{
Warn(source.GetType(), message, exception);
} /// <summary>
/// 警告信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Warn(Type source, object message, Exception exception)
{
GetLogger(source).Warn(message, exception);
} /// <summary>
/// 错误信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Error(object source, object message, Exception exception)
{
Error(source.GetType(), message, exception);
} /// <summary>
/// 错误信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Error(Type source, object message, Exception exception)
{
GetLogger(source).Error(message, exception);
} /// <summary>
/// 失败信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Fatal(object source, object message, Exception exception)
{
Fatal(source.GetType(), message, exception);
} /// <summary>
/// 失败信息
/// </summary>
/// <param name="source"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Fatal(Type source, object message, Exception exception)
{
GetLogger(source).Fatal(message, exception);
}
} }
LogHelper
Log4Net下载地址:http://logging.apache.org/log4net/