mvc中简单的异常记录

说明:异常处理

1.1 在WebApp的Model中 添加异常处理类 继承于HandleErrorAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBFJ.OA.WebApp.Models
{
    public class MyExceptionAttribute:HandleErrorAttribute
    {
        public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
        /// <summary>
        /// 捕获异常信息
        /// 别忘了在在异常处理过滤器中注册一下
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            //捕获异常信息
            Exception ex = filterContext.Exception;
            //写到日志时,多个线程同时访问,会造成并发,形成死锁===>写入队列
            ExceptionQueue.Enqueue(ex);
            //跳转到错误页
            filterContext.HttpContext.Response.Redirect("/Error.html");
        }
    }
}
MyExceptionAttribute

1.2 在WebApp下App_Start的FilterConfig异常处理过滤器中注册一下

using BBFJ.OA.WebApp.Models;
using System.Web;
using System.Web.Mvc;

namespace BBFJ.OA.WebApp
{
    public class FilterConfig
    {
        //在异常处理过滤器中注册一下
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new MyExceptionAttribute());
        }
    }
}
RegisterGlobalFilters

1.3 在WebApp中添加错误页面Error.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    服务器忙,5秒钟后跳转到首页!
</body>
</html>
View Code

1.4 在Global文件中开启线程

using BBFJ.OA.WebApp.Models;
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace BBFJ.OA.WebApp
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    //SpringMvcApplication继承了System.Web.HttpApplication
    //通过SpringMvcApplication中的ConfigureApplicationContext();方法,读取配置文件创建IHttpApplication容器
    public class MvcApplication : SpringMvcApplication //System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //开启线程,扫描异常 
            //建立日志文件夹,拿到物理路径
            string filePath = Server.MapPath("/Log/");
            //线程池中取线程
            ThreadPool.QueueUserWorkItem((a) => {
                while (true)
                {
                    //检查异常队列中是否有数据
                    if (MyExceptionAttribute.ExceptionQueue.Count() > 0)
                    {
                         Exception  ex= MyExceptionAttribute.ExceptionQueue.Dequeue();
                         if (ex != null)
                         {
                             //将异常写入日志文件
                             //文件名
                             string fileName = DateTime.Now.ToString("yyyy-MM-dd");
                             File.AppendAllText(filePath+fileName+".txt",ex.ToString(),System.Text.Encoding.UTF8);
                         }
                         else 
                         {
                             Thread.Sleep(3000);
                         }
                    }
                    else 
                    {
                        //很重要,如果队列中没有数据休息一会
                        Thread.Sleep(3000);
                    }
                }
            }, filePath);
        }
    }
}
View Code

1.5 在WebApp下建立日志文件夹

1.6 创建测试控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBFJ.OA.WebApp.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ShowResult()
        {
            int a = 2;
            int b = 0;
            int c = a / b;
            return Content(c.ToString());
        }
    }
}
TestController

运行结果


  日志信息:

 

posted @ 2017-03-27 20:14  逍遥小天狼  阅读(197)  评论(0编辑  收藏  举报