做项目,记录日志是免不了的。在开发过程中还可以调试,但是在项目发布之后,不可能长期这么做,日志则能够在最快的时间内发现问题。最近,在好几个项目中使用了Log4net,感觉确实给我省了不少力,总结一下使用吧,同时也留作以后备用。
1 使用Nuget安装Log4Net:
Install-Package log4net
2 创建LogHelper.cs
- using System;
- namespace UCsoft.Web.Common
- {
- /// <summary>
- /// Log4Net日志封装类 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- public class LogHelper
- {
- /// <summary>
- /// 信息标志
- /// </summary>
- private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
- /// <summary>
- /// 错误标志
- /// </summary>
- private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
- /// <summary>
- /// 调试标志
- /// </summary>
- private static readonly log4net.ILog logdebug = log4net.LogManager.GetLogger("logdebug");
- /// <summary>
- /// Log4Net信息记录封装 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public static void Info(string message)
- {
- if (loginfo.IsInfoEnabled)
- {
- loginfo.Info(message);
- }
- }
- /// <summary>
- /// Log4Net错误记录封装 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public static void Error(string message)
- {
- if (logerror.IsErrorEnabled)
- {
- logerror.Error(message);
- }
- }
- /// <summary>
- /// Log4Net错误记录封装 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- /// <param name="message"></param>
- /// <param name="ex"></param>
- /// <returns></returns>
- public static void Error(string message, Exception ex)
- {
- if (logerror.IsErrorEnabled)
- {
- if (!string.IsNullOrEmpty(message) && ex == null)
- {
- logerror.ErrorFormat("<br/>【附加信息】 : {0}<br>", new object[] {message});
- }
- else if (!string.IsNullOrEmpty(message) && ex != null)
- {
- string errorMsg = BeautyErrorMsg(ex);
- logerror.ErrorFormat("<br/>【附加信息】 : {0}<br>{1}", new object[] { message, errorMsg });
- }
- else if (string.IsNullOrEmpty(message) && ex != null)
- {
- string errorMsg = BeautyErrorMsg(ex);
- logerror.Error(errorMsg);
- }
- }
- }
- /// <summary>
- /// Log4Net调试记录封装 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public static void Debug(string message)
- {
- if (logdebug.IsErrorEnabled)
- {
- logdebug.Debug(message);
- }
- }
- /// <summary>
- /// Log4Net调试记录封装 2014-08-28 14:58:50 By 唐有炜
- /// </summary>
- /// <param name="message"></param>
- /// <param name="ex"></param>
- /// <returns></returns>
- public static void Debug(string message, Exception ex)
- {
- if (logdebug.IsDebugEnabled)
- {
- if (!string.IsNullOrEmpty(message) && ex == null)
- {
- logdebug.DebugFormat("<br/>【附加信息】 : {0}<br>", new object[] {message});
- }
- else if (!string.IsNullOrEmpty(message) && ex != null)
- {
- string errorMsg = BeautyErrorMsg(ex);
- logdebug.DebugFormat("<br/>【附加信息】 : {0}<br>{1}", new object[] { message, errorMsg });
- }
- else if (string.IsNullOrEmpty(message) && ex != null)
- {
- string errorMsg = BeautyErrorMsg(ex);
- logdebug.Debug(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(" ", "<br>");
- errorMsg = errorMsg.Replace("位置", "<strong style="color:red">位置</strong><br/>");
- return errorMsg;
- }
- }
- }
3 在
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
3 创建log4net.config
- <log4net>
- <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value="Log\" />
- <!-- 输出到什么目录-->
- <param name="AppendToFile" value="true" />
- <!-- 是否覆写到文件中-->
- <param name="StaticLogFileName" value="false" />
- <!-- 是否使用静态文件名-->
- <param name="DatePattern" value="yyyyMMdd".html"" />
- <!-- 日志文件名-->
- <param name="RollingStyle" value="Date" />
- <!--最小锁定模型以允许多个进程可以写入同一个文件-->
- <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="<HR COLOR=green>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日志标志:%c <BR> %n日志信息:%m <BR>%n <HR Size=1>" />
- </layout>
- <filter type="log4net.Filter.LevelRangeFilter">
- <levelMin value="DEBUG" />
- <levelMax value="INFO" />
- </filter>
- </appender>
- <appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value="Log\" />
- <param name="AppendToFile" value="true" />
- <param name="StaticLogFileName" value="false" />
- <param name="DatePattern" value="yyyyMMdd.error".html"" />
- <param name="RollingStyle" value="Date" />
- <!--最小锁定模型以允许多个进程可以写入同一个文件-->
- <param name="lockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
- <layout type="log4net.Layout.PatternLayout">
- <param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n日志标志:%c <BR> %n异常信息:%m <BR>%n <HR Size=1>" />
- </layout>
- <filter type="log4net.Filter.LevelRangeFilter">
- <levelMin value="ERROR" />
- <levelMax value="FATAL" />
- </filter>
- </appender>
- <root>
- <level value="ALL" />
- <appender-ref ref="LogFileAppender" />
- <appender-ref ref="ErrorFileAppender"/>
- </root>
- </log4net>
然后必须设置为复制到输出目录
4 测试
LogHelper.Debug("测试");
然后在Web根目录会生成一个Log文件夹,里面会有两个文件,一个一日期命名,一个错误文件