从网上下载的破解版的hishop中的源码扒出来的,觉得不错,先记下来,以后做项目异常BUG记录都用这个。。
下面是使用流程:
1. VS中用NUGET搜索log4net,安装
2. 项目的工具类库Util中建一Log类,代码如下:
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JiuFen.KuaiLe10.Util
{
public static class Log
{
public static void Error(object message)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Error(message);
}
public static void Error(object message, System.Exception exception)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Error(message, exception);
}
public static void Info(object message)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Info(message);
}
public static void Info(object message, System.Exception ex)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Info(message, ex);
}
public static void Warn(object message)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Warn(message);
}
public static void Warn(object message, System.Exception ex)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Warn(message, ex);
}
public static void Debug(object message)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Debug(message);
}
public static void Debug(object message, System.Exception ex)
{
ILog logger = LogManager.GetLogger(Log.GetCurrentMethodFullName());
logger.Debug(message, ex);
}
private static string GetCurrentMethodFullName()
{
string result;
try
{
int num = 2;
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
int num2 = stackTrace.GetFrames().Length;
System.Diagnostics.StackFrame frame;
string text;
do
{
frame = stackTrace.GetFrame(num++);
System.Type declaringType = frame.GetMethod().DeclaringType;
text = declaringType.ToString();
}
while (text.EndsWith("Exception") && num < num2);
string name = frame.GetMethod().Name;
result = text + "." + name;
}
catch
{
result = null;
}
return result;
}
}
}
3. web.config文件中加入如下内容:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<log4net>
<!--数据日志-->
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="LogInfo" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'Info.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!--错误日志-->
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="LogError" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'error.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!--调试日志-->
<appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="LogDebug" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'debug.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<!--文件形式记录日志-->
<appender-ref ref="ErrorRollingFileAppender" />
<appender-ref ref="DebugRollingFileAppender" />
<appender-ref ref="InfoRollingFileAppender" />
</root>
</log4net>
4. 建立基类控制 器,重写OnException方法:
using JiuFen.KuaiLe10.Util;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace JiuFen.KuaiLe10.Web.Controllers
{
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
Exception exception = filterContext.Exception;
string message = exception.Message;
base.OnException(filterContext);
string str = filterContext.RouteData.Values["controller"].ToString();
string str1 = filterContext.RouteData.Values["action"].ToString();
object item = filterContext.RouteData.DataTokens["area"];
string str2 = string.Format("页面未捕获的异常:Area:{0},Controller:{1},Action:{2}", item, str, str1);
Log.Error(str2, exception);
}
}
}
5. 最重要的,在你的公用类库所在的项目的AssemblyInfo.cs文件中加入:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
这样在建立控制器的时候继承自基类控制器,运行中出错的话都会记录到网站目录下的log目录下的debug目录下