mvc 使用预置队列类型存储异常对象

using PaiXie.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PaiXie.Pos.Admin 
{
    public class ExceptionFilterAttribute : HandleErrorAttribute
    {
        //使用预置队列类型存储异常对象
        public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
        public override void OnException(ExceptionContext filterContext) {
            //将异常信息入队
            ExceptionQueue.Enqueue(filterContext.Exception);
            //跳转到自定义错误页
            filterContext.HttpContext.Response.Redirect("~/Shared/Error");
            base.OnException(filterContext);
        }
    }
}
using PaiXie.Utils;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Web;

namespace PaiXie.Pos.Admin 
{
    public class MessageQueueConfig
    {
        public static void RegisterExceptionLogQueue()
        {
          //通过线程池开启线程,不停地从队列中获取异常信息并将其写入日志文件
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    #region 异常日志处理
                    try {
                        if (ExceptionFilterAttribute.ExceptionQueue.Count > 0) {
                            Exception ex = ExceptionFilterAttribute.ExceptionQueue.Dequeue(); //从队列中出队,获取异常对象
                            if (ex != null) {
                              //获得异常堆栈信息
                                string exceptionMsg = ex.ToString();
                                //将异常信息写入日志文件中
                                PlanLog.WriteLog(exceptionMsg, LogType.Error.ToString());
                            }
                        }
                        else {
                            Thread.Sleep(1000); //为避免CPU空转,在队列为空时休息1秒
                        }
                    }
                    catch (Exception ex) {
                        ExceptionFilterAttribute.ExceptionQueue.Enqueue(ex);
                    } 
                    #endregion
                }
            }, null
        
            );
        }
    }
}
原文地址:https://www.cnblogs.com/lyl6796910/p/5213351.html