zoukankan      html  css  js  c++  java
  • 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
            
                );
            }
        }
    }
  • 相关阅读:
    hdu4998 旋转坐标系
    hdu4998 旋转坐标系
    hdu5012 水搜索
    hdu5012 水搜索
    hdu5007 小水题
    ZOJ 3645 BiliBili 高斯消元 难度:1
    ZOJ 3654 Letty's Math Class 模拟 难度:0
    ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3
    ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2
    ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/5213351.html
Copyright © 2011-2022 走看看