做项目,记录日志是免不了的。在开发过程中还可以调试,但是在项目发布之后,不可能长期这么做,日志则能够在最快的时间内发现问题。最近,在好几个项目中使用了Log4net,感觉确实给我省了不少力,总结一下使用吧,同时也留作以后备用。 1 使用Nuget安装Log4Net: Install-Package log4net
class="alignnone wp-image-5628" src="/Upload/Images/2014103121/2B5C53FDF627EF41.png" alt="QQ截图20141031182038" width="592" height="365" />
2 创建LogHelper.cs
- using System;
- namespace UCsoft.Web.Common
- {
-
-
-
- public class LogHelper
- {
-
-
-
- private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
-
-
-
- private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
-
-
-
- private static readonly log4net.ILog logdebug = log4net.LogManager.GetLogger("logdebug");
-
-
-
-
-
- public static void Info(string message)
- {
- if (loginfo.IsInfoEnabled)
- {
- loginfo.Info(message);
- }
- }
-
-
-
-
-
- public static void Error(string message)
- {
- if (logerror.IsErrorEnabled)
- {
- logerror.Error(message);
- }
- }
-
-
-
-
-
-
- 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);
- }
- }
- }
-
-
-
-
-
- public static void Debug(string message)
- {
- if (logdebug.IsErrorEnabled)
- {
- logdebug.Debug(message);
- }
- }
-
-
-
-
-
-
- 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);
- }
- }
- }
-
-
-
-
-
- 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><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文件夹,里面会有两个文件,一个一日期命名,一个错误文件
original="http://bcs.duapp.com/terwer/blog/2014/10/QQ截图20141031190317.png" />
最后,阅读更多内容 http://www.terwer.com/net-log4net-logging-framework-summary.html