一.在AssemblyInfo.cs文件添加以下内容
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ConfigFile//log4net.config", Watch = true)]
二.新增ConfigFile文件夹,将log4net.config配置文件信息放入
log4net.config 配置信息如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <!--定义输出到文件中--> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender"> <!--日志的路径--> <file value="Logs/" /> <!--是否覆盖,默认是追加true--> <appendToFile value="true"/> <rollingStyle value="Composite"/> <!--文件名称--> <DatePattern value="yyyyMMdd‘.log‘"></DatePattern> <!--设置无限备份=-1 ,最大备份数为1000--> <param name="MaxSizeRollBackups" value="100"/> <!--每个文件的大小--> <param name="MaximumFileSize" value="500KB"/> <!--名称是否可以更改 为false为可以更改--> <param name="StaticLogFileName" value="false"/> <layout type="log4net.Layout.PatternLayout"> <!--输出格式--> <conversionPattern value="%n【记录时间】%date%n【描述】%message%n"/> </layout> </appender> <!--定义输出到控制台命令行中--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n【记录时间】%date%n【描述】%message%n"/> </layout> </appender> <!--定义输出到windows事件中--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n========== %n【日志级别】%-5level %n【记录时间】%date %n【线程编号】[%thread] %n【执行时间】[%r]毫秒 %n【出错文件】%F %n【出错行号】%L %n【出错的类】%logger 属性[%property{NDC}] %n【错误描述】%message %n【错误详情】%newline"/> </layout> </appender> <!--定义输出到Trace中--> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />--> <conversionPattern value="%n========== %n【日志级别】%-5level %n【记录时间】%date %n【线程编号】[%thread] %n【执行时间】[%r]毫秒 %n【出错文件】%F %n【出错行号】%L %n【出错的类】%logger 属性[%property{NDC}] %n【错误描述】%message %n【错误详情】%newline"/> </layout> </appender> <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。--> <root> <!--日志的级别 OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL 只有日志输出级别大于或等于配置的级别才能输出日志信息--> <level value="ALL" /> <!--文件形式记录日志--> <appender-ref ref="SysAppender"/> <!--控制台控制显示日志--> <appender-ref ref="ConsoleAppender"/> <!--Windows事件日志--> <!--<appender-ref ref="EventLogAppender"/>--> <!--TraceAppender日志--> <!--<appender-ref ref="TraceAppender"/>--> </root> </log4net> </configuration>
三.新增log4NetUtil.CS帮助封装类,代码如下:
public class log4NetUtil { #region 变量 //定义信息的二次处理 private static event Action<string> OutputMessage; //ILog对象 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endregion #region 定义信息二次处理方式 private static void HandMessage(object Msg) { if (OutputMessage != null) { OutputMessage(Msg.ToString()); } } private static void HandMessage(object Msg, Exception ex) { if (OutputMessage != null) { OutputMessage(string.Format("{0}:{1}", Msg.ToString(), ex.ToString())); } } private static void HandMessage(string format, params object[] args) { if (OutputMessage != null) { OutputMessage(string.Format(format, args)); } } #endregion #region 封装Log4net public static void Debug(object message) { HandMessage(message); if (log.IsDebugEnabled) { log.Debug(message); } } public static void Debug(object message, Exception ex) { HandMessage(message, ex); if (log.IsDebugEnabled) { log.Debug(message, ex); } } public static void DebugFormat(string format, params object[] args) { HandMessage(format, args); if (log.IsDebugEnabled) { log.DebugFormat(format, args); } } public static void Error(object message) { HandMessage(message); if (log.IsErrorEnabled) { log.Error(message); } } public static void Error(object message, Exception ex) { HandMessage(message, ex); if (log.IsErrorEnabled) { log.Error(message, ex); } } public static void ErrorFormat(string format, params object[] args) { HandMessage(format, args); if (log.IsErrorEnabled) { log.ErrorFormat(format, args); } } public static void Fatal(object message) { HandMessage(message); if (log.IsFatalEnabled) { log.Fatal(message); } } public static void Fatal(object message, Exception ex) { HandMessage(message, ex); if (log.IsFatalEnabled) { log.Fatal(message, ex); } } public static void FatalFormat(string format, params object[] args) { HandMessage(format, args); if (log.IsFatalEnabled) { log.FatalFormat(format, args); } } public static void Info(object message) { HandMessage(message); if (log.IsInfoEnabled) { log.Info(message); } } public static void Info(object message, Exception ex) { HandMessage(message, ex); if (log.IsInfoEnabled) { log.Info(message, ex); } } public static void InfoFormat(string format, params object[] args) { HandMessage(format, args); if (log.IsInfoEnabled) { log.InfoFormat(format, args); } } public static void Warn(object message) { HandMessage(message); if (log.IsWarnEnabled) { log.Warn(message); } } public static void Warn(object message, Exception ex) { HandMessage(message, ex); if (log.IsWarnEnabled) { log.Warn(message, ex); } } public static void WarnFormat(string format, params object[] args) { HandMessage(format, args); if (log.IsWarnEnabled) { log.WarnFormat(format, args); } } #endregion }