• 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;
            }
        }
    }
  • 相关阅读:
    stack, deque 和 queue的对比
    Android 修改圆形progressBar颜色
    java.lang.OutOfMemoryError: Failed to allocate a 3110419 byte allocation with 741152 free bytes and
    Android GreenDAO 3.0 不修改版本号的情况下增加、删除表、添加字段
    Android监听安装卸载
    Android实现异步的几种方法
    Android GreenDao清空数据库的方法
    Android 6.0 动态权限申请
    Android无需权限显示悬浮窗
    Android 极光推送JPush---自定义提示音
  • 原文地址:https://www.cnblogs.com/ecollab/p/6155573.html
Copyright © 2020-2023  润新知