• 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");
            }
    
        }
    }
  • 相关阅读:
    python Asyncore.dispatcher 理解
    COCOA® PROGRAMMING FOR MAC® OS X (2)- Speak Line
    COCOA® PROGRAMMING FOR MAC® OS X (1)- Get Start
    Objetive-C initialize研究
    Objetive-C +load方法研究
    Python创建多进程,用Queue传递信息
    Python 中的GIL
    IOS CrackMe 破解学习
    越狱手机发送短信
    Vue 子组件向父组件传参
  • 原文地址:https://www.cnblogs.com/zjbky/p/5732592.html
Copyright © 2020-2023  润新知