码迷,mamicode.com
首页 > Windows程序 > 详细

C# Mvc异常处理过滤器

时间:2016-08-03 14:54:28      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:

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("\r\n-------------  异常信息   ---------------------------------------------------------------");
            _builder.Append("\r\n发生时间:" + DateTime.Now.ToString());
            _builder.Append("\r\n发生异常页:" + httpContext.Url.ToString());
            _builder.Append("\r\n异常信息:" + ex.Message);
            _builder.Append("\r\n错误源:" + ex.Source);
            _builder.Append("\r\n堆栈信息:" + ex.StackTrace);
            _builder.Append("\r\n-----------------------------------------------------------------------------------------\r\n");
            //日志物理路径

            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");
        }

    }
}

 

C# Mvc异常处理过滤器

标签:

原文地址:http://www.cnblogs.com/zjbky/p/5732592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!