第一步在项目中找到App_Start文件夹下建立一个错误日志过滤器。

第二步在Global.asax文件中注册下日志过滤器

第三步: 继承一个ExceptionFilterAtrribute

第四步:重写基类

/// <summary>
/// 重写基类的异常处理方法
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//1.异常日志记录(正式项目里面一般是用log4net记录异常日志)
//HttpContext.Current.Response.Write(
var LogName = "Log";
var FileAddress = "Log.txt";
var path = HttpContext.Current.Server.MapPath("~/");
string[] temp = path.Split("\\".ToCharArray());
string LevelPath = "";
for (int i = 0; i < temp.Length - 2; i++)
{
LevelPath += temp[i];
LevelPath += "\\";
}
if (!Directory.Exists(LevelPath + "\\" + LogName))//如果不存在就创建file文件夹
{
Directory.CreateDirectory(LevelPath + "\\" + LogName);
}

FileStream fs = null;
if (!File.Exists(LevelPath + "\\" + LogName + "\\" + FileAddress))
{
fs = new FileStream(LevelPath + "\\" + LogName + "\\" + FileAddress, FileMode.Create);
}
else
{
fs = new FileStream(LevelPath + "\\" + LogName + "\\" + FileAddress, FileMode.Append);
}
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("------------------------------------");
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
sw.WriteLine("异常信息:" + actionExecutedContext.Exception.GetType().ToString() + "||" + actionExecutedContext.Exception.Message);
sw.WriteLine("堆栈信息:" + actionExecutedContext.Exception.StackTrace);
sw.WriteLine("------------------------------------");

//var d = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "——" +
// actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + "——堆栈信息:" +
// actionExecutedContext.Exception.StackTrace;
////);

//2.返回调用方具体的异常信息 501 不支持请求的函数
if (actionExecutedContext.Exception is NotImplementedException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
//错误码 408 超时
else if (actionExecutedContext.Exception is TimeoutException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
}
//错误码 403 拒绝访问
else if (actionExecutedContext.Exception is NotImplementedException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);

}
//错误码404
else if (actionExecutedContext.Exception is NotImplementedException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound);

}

//.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
else
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
}

base.OnException(actionExecutedContext);
}

注:  我不知道我描述的清不清楚, 实在看不懂 代码粘贴过去就可以用。

最新文章

  1. Chrome立体动画代码
  2. 深入了解jQuery之整体架构
  3. eclipse导入JDK源码
  4. 权限框架 - shiro 自定义realm
  5. html 鼠标移入标签 显示小手指
  6. CentOS6 PXE+Kickstart无人值守安装
  7. DevExpress控件XtraGrid的Master-Detail用法 z
  8. word2007 每页显示表头
  9. postgresql9.5 run 文件linux安装后配置成开机服务
  10. Spring Mvc和Mybatis的多数据库访问配置过程
  11. linux登录windows服务器
  12. Ubuntu安装MongoDB和PHP扩展
  13. 对于Javascript 执行上下文的理解
  14. mybatis通用mapper的使用
  15. linux基础命令学习笔记(二)
  16. C++ 一些特性
  17. SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统
  18. JavaEE XML的读写(利用JDom对XML文件进行读写)
  19. 十大经典排序算法的 JavaScript 实现
  20. ES6 class的基本语法-学习笔记

热门文章

  1. Python小白学习之路(十七)—【内置函数二】
  2. 使用sqlyog连接到服务器数据库,实现可视化数据操作。(完美解决版。)《亲测!!!!》
  3. 【NOIP2017】逛公园 最短路+DP
  4. 【BZOJ3625】【codeforces438E】小朋友和二叉树 生成函数+多项式求逆+多项式开根
  5. 【2018北京集训十二】 coin 矩阵快速幂
  6. 搭建互联网架构学习--003--maven以及nexus私服搭建
  7. Picasso加载网络图片失败,提示decodestream时返回null
  8. OkHttp3实现Cookies管理及持久化
  9. Git 命令操作记录
  10. 笛卡尔积算法的sku