Mvc异常处理器

using System;
using System.Text;
using EMS.Domains.Core;
using System.Web.Mvc;
using Json.Net;
using Util.Webs;
using Json = Util.Json;

namespace EMS.MvcUI.Base
{
    public class FilterExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            var isAjax = filterContext.HttpContext.Request.IsAjaxRequest();
            if (isAjax)
            {
                if (filterContext.Exception is ShowException)
                {
                    filterContext.Result = new ContentResult
                    {
                        Content =
                            JsonConvert.SerializeObject(
                                new {Code = StateCode.Fail, Message = filterContext.Exception.Message, Data = ""})
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult
                    {
                        Content =
                            JsonConvert.SerializeObject(new {Code = StateCode.Fail, Message = "服务器异常,请联系管理员", Data = ""})
                    };
                }
            }
            if (!(filterContext.Exception is ShowException))
            {
                WriteLog(filterContext);
            }
            filterContext.ExceptionHandled = true;
        }

        private Exception BuildErrorMessage(Exception ex)
        {
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
            }
            return ex;
        }

        private void WriteLog(ExceptionContext filterContext)
        {
            var httpContext = filterContext.RequestContext.HttpContext.Request;
            var ex = BuildErrorMessage(filterContext.Exception);
            // 在出现未处理的错误时运行的代码
            StringBuilder _builder = new StringBuilder();
            _builder.Append("
-------------  异常信息   ---------------------------------------------------------------");
            _builder.Append("
发生时间:" + DateTime.Now.ToString());
            _builder.Append("
发生异常页:" + httpContext.Url.ToString());
            _builder.Append("
异常信息:" + ex.Message);
            _builder.Append("
错误源:" + ex.Source);
            _builder.Append("
堆栈信息:" + ex.StackTrace);
            _builder.Append("
-----------------------------------------------------------------------------------------
");
            //日志物理路径

            DateTime date = DateTime.Now;
            string path = httpContext.MapPath("~/Log/");
            string month = date.ToString("yyyy-MM");
            if (!System.IO.Directory.Exists(path + month))
                System.IO.Directory.CreateDirectory(path + month);
            string currentDate = date.ToString("yyyy-MM-dd");
            string savePath = path + month + "/" + currentDate + ".log";
            System.IO.File.AppendAllText(savePath, _builder.ToString(), System.Text.Encoding.Default);
            //filterContext.RequestContext.HttpContext.Server.ClearError();
            //filterContext.ExceptionHandled = true;

            //filterContext.RequestContext.HttpContext.Response.Redirect("/Home/Error");
        }

    }
}
原文地址:https://www.cnblogs.com/NotEnough/p/8599498.html