log4net使用流程

从网上下载的破解版的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;
    }
}

}

  1. web.config文件中加入如下内容:



























































  1. 建立基类控制 器,重写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);
    }

}

}

  1. 最重要的,在你的公用类库所在的项目的AssemblyInfo.cs文件中加入:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

这样在建立控制器的时候继承自基类控制器,运行中出错的话都会记录到网站目录下的log目录下的debug目录下

你可能感兴趣的:(log4net使用流程)