返回首页

高速

快速搭建log4net

1.安装

使用nuget下载log4net 当前最新2.0.8

2.配置项目程序集

在Assembly程序集中增加如下
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

3.在log4net.config文件中配置如下 如是站点应放到bin文件夹下

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <!-- 错误日志类--> <logger name="logerror"> <level value="ALL" /> <appender-ref ref="ErrorAppender" /> </logger> <!-- 信息日志类 --> <logger name="loginfo"> <level value="ALL" /> <appender-ref ref="InfoAppender" /> </logger> <!-- 错误日志附加介质--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\Error\\" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="100" /> <param name="MaxFileSize" value="10240" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" /> <param name="RollingStyle" value="Date" /> <!--布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n【异常时间】:%d [%t] &lt;BR&gt;%n【异常级别】:%-5p &lt;BR&gt;%n【异 常 类】:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" /> </layout> </appender> <!-- 信息日志附加介质--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\Info\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" /> <param name="RollingStyle" value="Date" /> <!-- 信息日志布局--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p &lt;BR&gt;%n日 志 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;" /> </layout> </appender> </log4net> </configuration>

4.创建帮助类

namespace Common { /// <summary> /// 日志帮助类 /// </summary> public class LogHelper { //GetLogger表示log4net配置文件中logger标签中name属性,此处要一致 不然无log输出 private static readonly log4net.ILog logInfo = log4net.LogManager.GetLogger("loginfo"); private static readonly log4net.ILog logError = log4net.LogManager.GetLogger("logerror"); /// <summary> /// 记录Info日志 /// </summary> /// <param name="msg"></param> /// <param name="ex"></param> public static void Info(string msg) { if (logInfo.IsInfoEnabled) { logInfo.Info(msg); } } /// <summary> /// 记录Error日志 /// </summary> /// <param name="errorMsg"></param> /// <param name="ex"></param> public static void Error(string info, Exception ex = null) { if (!string.IsNullOrEmpty(info) && ex == null) { logError.ErrorFormat("【附加信息】 : {0}<br>", new object[] { info }); } else if (!string.IsNullOrEmpty(info) && ex != null) { string errorMsg = BeautyErrorMsg(ex); logError.ErrorFormat("【附加信息】 : {0}<br>{1}", new object[] { info, errorMsg }); } else if (string.IsNullOrEmpty(info) && ex != null) { string errorMsg = BeautyErrorMsg(ex); logError.Error(errorMsg); } } /// <summary> /// 美化错误信息 /// </summary> /// <param name="ex">异常</param> /// <returns>错误信息</returns> private static string BeautyErrorMsg(Exception ex) { string errorMsg = string.Format("【异常类型】:{0} <br>【异常信息】:{1} <br>【堆栈调用】:{2}", new object[] { ex.GetType().Name, ex.Message, ex.StackTrace }); errorMsg = errorMsg.Replace("\r\n", "<br>"); errorMsg = errorMsg.Replace("位置", "<strong style=\"color:red\">位置</strong>"); return errorMsg; } } }