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

Mvc异常处理器的更多相关文章

  1. 012医疗项目-模块一:统一异常处理器的设计思路及其实现(涉及到了Springmvc的异常处理流程)

    我们上一篇文章是建立了一个自定义的异常类,来代替了原始的Exception类.在Serice层抛出异常,然后要在Action层捕获这个异常,这样的话在每个Action中都要有try{}catch{}代 ...

  2. spring mvc 异常统一处理方式

    springMVC提供的异常处理主要有两种方式: 一种是直接实现自己的HandlerExceptionResolver: 另一种是使用注解的方式实现一个专门用于处理异常的Controller——Exc ...

  3. 关于SpringMVC的全局异常处理器

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  4. SpringMVC框架笔记02_参数绑定返回值文件上传异常处理器JSON数据交互_拦截器

    目录 第1章 高级参数的绑定 1.1 参数的分类 1.2 数组类型的参数的绑定 1.3 集合类型的参数的绑定 第2章 @RequestMapping的用法 2.1 URL路径映射 2.2 请求方法限定 ...

  5. 基于SpringMVC的全局异常处理器介绍(转)

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  6. SpringMVC实现全局异常处理器 (转)

    出处:  SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...

  7. 【SpringMVC】文件上传与下载、拦截器、异常处理器

    文件下载 使用ResponseEntity实现下载文件的功能 index.html <!DOCTYPE html> <html lang="en" xmlns:t ...

  8. spring设置全局异常处理器

    1.spring设置全局异常,它的原理是向上捕获 spring.xml配置 <!--自定义全局异常处理器--> <bean id="globalExceptionResol ...

  9. Java 线程异常处理器

    Thread.UncaughtExceptionHandler 是Thread类的一个静态内部接口,该接口只有一个方法: void uncaughtException(Thread t, Throwa ...

随机推荐

  1. 集训Day10

    果然颓的不像话 bzoj3680 gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty.gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了.蒟蒻们将n个gty吊在n根绳子上,每根绳子穿过天 ...

  2. python 动态添加属性及方法及“__slots__的作用”

    1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...

  3. 1111 Online Map (30)(30 分)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  4. poj3709 K-Anonymous Sequence[贪心+斜率优化dp]

    地址 n个数,可进行把一个数减小的操作,代价为减小的值.现求使数列任意一个数都存在至少k-1个数和他相同,问操作的最小代价. 可以先考虑最小的数,由于只能减,所以必须得至少k-1个数减为最小数,贪心策 ...

  5. AtCoder Grand Contest 014 D:Black and White Tree

    题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...

  6. BZOJ2028:[SHOI2009]会场预约(平衡树版)

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  7. XmlSerialize error: There was an error generating the XML document.

    今天遇到一个很火的问题, 一个c#的class 序列化成xml后抛出异常, 信息为: XmlSerialize error: There was an error generating the XML ...

  8. python3 + selenium + eclipse 中报错:'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

    解决:提示chrome driver没有放置在正确的路径下,于是下载chrome dirver,然后放置到C:\Python36的目录下,再次运行就OK了!

  9. CodeForces 287B Pipeline (水题)

    B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  10. 安装coreseek 编译错误

    Ubuntu下 编译csft 报错fatal error python.h no such file or directory 执行apt-get install python-dev即可