C# Log4net根据日志等级输出到不同文件
原文地址:
Log4Net.Config
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!--添加自定义节点:log4net type:解析类名,程序集名(log4net.dll)--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <root> <!--<level value="DEBUG"/>--> <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF--> <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录--> <level value="ALL"/> <!--根据log级别记录到不同的日志文件--> <appender-ref ref="ErrorLog" /> <appender-ref ref="InfoLog" /> </root> <!--Error--> <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender"> <file value="Log/"/> <appendToFile value="true" /> <rollingStyle value="Date" /> <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> <DatePattern value="yyyyMMdd"_Error.log"" /> <!--日志文件名是否为静态--> <StaticLogFileName value="false"/> <!--多线程时采用最小锁定--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--布局(向用户显示最后经过格式化的输出信息)--> <layout type="log4net.Layout.PatternLayout"> <ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> </layout> <securityContext type="log4net.Util.WindowsSecurityContext"> <credentials value="Process" /> </securityContext> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> <!--Error--> <!--Info--> <appender name="InfoLog" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <file value="Log/"/> <appendToFile value="true" /> <rollingStyle value="Date" /> <!--日志文件名是否为静态--> <StaticLogFileName value="false"/> <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置--> <DatePattern value="yyyyMMdd"_Info.log"" /> <!--多线程时采用最小锁定--> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--布局(向用户显示最后经过格式化的输出信息)--> <layout type="log4net.Layout.PatternLayout"> <ConversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> </layout> <securityContext type="log4net.Util.WindowsSecurityContext"> <credentials value="Process" /> </securityContext> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="INFO" /> <levelMax value="INFO" /> </filter> </appender> <!--Info--> </log4net> </configuration>
LogHelper:
using System; using System.Collections.Concurrent; using log4net; // 指定log4net使用的config文件来读取配置信息 [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Log4Net.config", Watch = true)] namespace EnterpriseServer.Extensions { public class LogHelper { private static readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>(); /// <summary> /// 获取记录器 /// </summary> /// <param name="source">soruce</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">source</param> /// <param name="message">message</param> public static void Debug(object source, string message) { Debug(source.GetType(), message); } /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="ps">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">source</param> /// <param name="message">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">source</param> /// <param name="message">message</param> public static void Info(object source, object message) { Info(source.GetType(), message); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">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">source</param> /// <param name="message">message</param> public static void Warn(object source, object message) { Warn(source.GetType(), message); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">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">source</param> /// <param name="message">message</param> public static void Error(object source, object message) { Error(source.GetType(), message); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">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">source</param> /// <param name="message">message</param> public static void Fatal(object source, object message) { Fatal(source.GetType(), message); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">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">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Debug(object source, object message, Exception exception) { Debug(source.GetType(), message, exception); } /// <summary> /// 调试信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Debug(Type source, object message, Exception exception) { GetLogger(source).Debug(message, exception); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Info(object source, object message, Exception exception) { Info(source.GetType(), message, exception); } /// <summary> /// 关键信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Info(Type source, object message, Exception exception) { GetLogger(source).Info(message, exception); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Warn(object source, object message, Exception exception) { Warn(source.GetType(), message, exception); } /// <summary> /// 警告信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Warn(Type source, object message, Exception exception) { GetLogger(source).Warn(message, exception); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Error(object source, object message, Exception exception) { Error(source.GetType(), message, exception); } /// <summary> /// 错误信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Error(Type source, object message, Exception exception) { GetLogger(source).Error(message, exception); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Fatal(object source, object message, Exception exception) { Fatal(source.GetType(), message, exception); } /// <summary> /// 失败信息 /// </summary> /// <param name="source">source</param> /// <param name="message">message</param> /// <param name="exception">ex</param> public static void Fatal(Type source, object message, Exception exception) { GetLogger(source).Fatal(message, exception); } } }
纸上得来终觉浅,绝知此事要躬行。