MVC异常日志生产者消费者模式记录(异常过滤器)

生产者消费者模式

定义自己的异常过滤器并注册

namespace Eco.Web.App.Models
{
    public class MyExceptionAttribute : HandleErrorAttribute
    {
        public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            ExceptionQueue.Enqueue(filterContext.Exception);//将异常信息添加到队列中。
            filterContext.HttpContext.Response.Redirect("/Error.html");

        }
    }
}
namespace Eco.Web.App
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new MyExceptionAttribute());
        }
    }
}

开线程写异常信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Spring.Web.Mvc;

namespace Spring.Mvc4QuickStart
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : SpringMvcApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            string fileLogPath = Server.MapPath("/Log/");
            //WaitCallback
            ThreadPool.QueueUserWorkItem((a) =>
            {
                while (true)
                {
                    if (MyExceptionAttribute.ExceptionQueue.Count > 0)
                    {
                        Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();//出队
                                                                                     //string fileName = DateTime.Now.ToString("yyyy-MM-dd")+".txt";
                                                                                     //File.AppendAllText(fileLogPath + fileName, ex.ToString(), System.Text.Encoding.Default);
                        ILog logger = LogManager.GetLogger("errorMsg");
                        logger.Error(ex.ToString());
                    }
                    else
                    {
                        Thread.Sleep(3000);//如果队列中没有数据,休息避免造成CPU的空转.
                    }
                }


            }, fileLogPath);
        }

        protected override System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver()
        {
            //get the 'default' resolver, populated from the 'main' config metadata
            var resolver = base.BuildWebApiDependencyResolver();

            //check if its castable to a SpringWebApiDependencyResolver
            var springResolver = resolver as SpringWebApiDependencyResolver;

            //if it is, add additional config sources as needed
            if (springResolver != null)
            {
                springResolver.AddChildApplicationContextConfigurationLocation("file://~/Config/child_controllers.xml");
            }

            //return the fully-configured resolver
            return resolver;
        }
    }
}
原文地址:https://www.cnblogs.com/ecollab/p/6155573.html