• C# 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");
            }
    
        }
    }
  • 相关阅读:
    [考试反思]0904NOIP模拟测试37:守望
    游戏:最短路,拆点
    [考试反思]0903NOIP模拟测试36:复始
    [考试反思]0902NOIP模拟测试35:摆动
    长寿花:dp
    [考试反思]0901NOIP模拟测试34:游离
    赤壁情:dp
    [考试反思]0829NOIP模拟测试33:仰望
    [考试反思]0828NOIP模拟测试32:沉底
    宅男计划:单峰函数三分
  • 原文地址:https://www.cnblogs.com/zjbky/p/5732592.html
Copyright © 2020-2023  润新知