第一步在项目中找到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);
}

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

mvc里全局错误日志的更多相关文章

  1. Asp.Net MVC中记录错误日志保存到本地txt文件

    为了方便查询系统出错弄个错误日志出来对于维护运维来说是很有必要的. 1.在Asp.Net MVC项目中的App_Start添加一个用于处理异常类的文件ErrorLog让他继承HandleErrorAt ...

  2. asp.net 使用HttpModule记录全局错误

    以前使用Global.asax记录全局的错误日志觉得挺好用,但是如果一个解决方案下有N多个项目,每个下边都需要加一个并且代码都还是重复的,终于有一天无法再忍受这种模式,考虑到HttpModule,直接 ...

  3. MVC自定义错误日志异常处理

    MVC添加错误日志处理模块很简单,只要写个继承自HandleErrorAttribute的过滤器,重新OnException方法,贴个异常处理代码如下: public class ExceptionA ...

  4. asp.net mvc中加入log4net记录错误日志

    直接上代码示例:https://share.weiyun.com/aff36f2547514cfefe129ebb8ccb28ef 首先添加加log4net的dll,推荐用nuget.... 贴上配置 ...

  5. asp.net mvc 3 配置全局错误处理 Web.config中设置CustomError

    摘自: http://www.myexception.cn/web/1130191.html asp.net mvc 配置全局异常处理 Web.config中设置CustomError Web.con ...

  6. C# MVC 全局错误Application_Error中处理(包括Ajax请求)

    在MVC的Global.asax Application_Error 中处理全局错误. 如果在未到创建请求对象时报错,此时 Context.Handler == null . 判断为Ajax请求时,我 ...

  7. ASP.NET MVC中错误日志信息记录

    MVC中有一个处理异常的过滤器 HandleErrorAttribute 1.新建一个类继承自 HandleErrorAttribute,然后重写OnException这个方法 public clas ...

  8. 【手把手教你Elmah】如何在MVC.NET项目中在线查看【错误日志】

     一.  在NuGet下载Elmah.MVC dll文件!  或者点击下载dll文件,并且引用客户端. 二.配置WebConfig <sectionGroup name="elmah& ...

  9. Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志

    在使用SimpleMappingExceptionResolver实现统一异常处理后(参考Spring MVC的异常统一处理方法), 发现出现异常时,log4j无法在控制台输出错误日志.因此需要自定义 ...

随机推荐

  1. pythonweb框架Flask学习笔记03-变量规则

    #-*- coding:utf-8 -*- from flask import Flask app=Flask(__name__) @app.route('/post/<int:postid&g ...

  2. TestNG参数

    dependsOnMethods可以使测试用例按某个特定的顺序执行 实例: @Test(dependsOnMethods=“s”) public  void  a() System.out.print ...

  3. iOS 枚举讲解

    枚举增强程序的可读性,用法上还是需要注意的 1.C语言的写法 enum XMPPReconnectFlags { kShouldReconnect = 1 << 0, // If set, ...

  4. openssl生成RSA格式的公私钥,并转为pkcs8格式

    第一步:生成私钥,这里我们指定私钥的长度为2048 openssl genrsa -out rsa_private_key.pem 2048 第二步:根据私钥生成对应的公钥: openssl rsa ...

  5. php如何使用rabbitmq实现发布消息和消费消息(一对多)(tp框架)(第二篇)

    一个publisher发布消息  多个个customer接受消息 1:准备工作参照: http://www.cnblogs.com/spicy/p/7886820.html 2,:路由: 3: 方法: ...

  6. Flow中的Switch分析

    A switch statement can complete normally iff at least one of the following is true: (1)The switch bl ...

  7. 【转】JVM(Java虚拟机)优化大全和案例实战

    原文地址:http://blog.csdn.net/kthq/article/details/8618052 堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Sp ...

  8. cookie和session的区别,session的生命周期,

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...

  9. redis实战笔记(3)-第3章 Redis命令

    第3章 Redis命令   本章主要内容 字符串命令. 列表命令和集合命令 散列命令和有序集合命令 发布命令与订阅命令 其他命令   在每个不同的数据类型的章节里, 展示的都是该数据类型所独有的. 最 ...

  10. IDF实验室—不难不易的js加密

    查看源代码 <html> <head> <script src="/tpl/wctf/Public/js/lib/jquery.js">< ...