最近监控线上日志,网站是ASP.NET MVC 开发的,发现不少错误日志都记录同样的内容:

The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Search/Error.aspx
~/Views/Search/Error.ascx
~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx
~/Views/Search/Error.cshtml
~/Views/Search/Error.vbhtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml
System.Web.Mvc
at System.Web.Mvc.ViewResult.FindView(ControllerContext context)

本地调试下不会发现,通过排查发现本地和线上配置的差异在于 customErrors mode="On",将本地改后单点调试发现确实是将这样的错误记录,而本身我在action里面 throw 出来的异常已经抓取不到了。也就是说真的异常并未抓取到,而记录的是一个无关紧要莫名其妙的日志,因为我并没有任何代码去返回”Error“这个view。

先说下目前我抓取异常的方式:在Global.asax文件中增加

        protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
//Write Log
}

假如在Controller里面去override OnException方法进行写日志则不会出现这样的问题,但是在Controller里记日志可能会使在View里面出错的日志记录不到。

烦了很久,最后不经意发现MVC里面会默认注册一个Filter,代码如下

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}

注册掉可以解决上述问题,但是我不太服气,于是在codeplex翻了mvc的源代码,HandleErrorAttribute的代码如下:

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.Mvc.Properties; namespace System.Web.Mvc
{
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is AllowMultiple = true and users might want to override behavior.")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class HandleErrorAttribute : FilterAttribute, IExceptionFilter
{
private const string DefaultView = "Error"; private readonly object _typeId = new object(); private Type _exceptionType = typeof(Exception);
private string _master;
private string _view; public Type ExceptionType
{
get { return _exceptionType; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (!typeof(Exception).IsAssignableFrom(value))
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
MvcResources.ExceptionViewAttribute_NonExceptionType, value.FullName));
} _exceptionType = value;
}
} public string Master
{
get { return _master ?? String.Empty; }
set { _master = value; }
} public override object TypeId
{
get { return _typeId; }
} public string View
{
get { return (!String.IsNullOrEmpty(_view)) ? _view : DefaultView; }
set { _view = value; }
} public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
} // If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
} Exception exception = filterContext.Exception; // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != )
{
return;
} if (!ExceptionType.IsInstanceOfType(exception))
{
return;
} string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = ; // Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
}

HandleErrorAttribute

坑爹,他mvc里面会默认返回error视图,看来以后新开发时还是创建空项目好了。

至于为啥设置了customErrors mode="On"后才会出现这种垃圾日志,则要归咎于HandleErrorAttribute类的OnException方法里面了

        // If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}

解决方案是删掉默认注册的这个filter或者新增类去override相应的方法。

记处理线上记录垃圾日志 The view 'Error' or its master was not found的更多相关文章

  1. 处理线上CPU负载过高的故障现象

    如何处理线上CPU100%的故障现象 处理流程: 1.登陆线上机器top命令,查看耗费cpu的进程号,举例来说发现进程24008持续耗费资源 2.top -Hp 24008去查看持续耗费cpu的线程号 ...

  2. Spring Boot中使用AOP记录请求日志

    这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...

  3. Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中

    Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中 在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail  ...

  4. 使用SpringBoot AOP 记录操作日志、异常日志

    平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...

  5. DB2不记录事务日志

    1. DB2大数据处理不记录事务日志步骤:  建表需要添加属性“NOT LOGGED INITIALLY”  在大批量更改操作的同一个事务开始时执行:“ALTER TABLE tabname ACTI ...

  6. 记录php日志

    1.记录PHP错误日志 display_errors与log_errors的区别 display_errors 错误回显,一般常用于开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴 ...

  7. StackTrace堆栈跟踪记录详细日志

    使用StackTrace堆栈跟踪记录详细日志(可获取行号) 2014-04-25 22:30 by 螺丝钉想要螺丝帽, 350 阅读, 3 评论, 收藏, 编辑 上一篇我们提到使用.NET自带的Tra ...

  8. C#中四步轻松使用log4net记录本地日志

    在这里,记录我在项目中使用log4net记录本地日志的步骤.在不会之前感觉很难,很神秘,一旦会了之后其实没那么难.其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验. 第一步:首先从 ...

  9. Laravel技巧之记录多日志

    相信每个小伙伴在使用laravel的时候都会记录日志.查看日志.那么问题来了,比如我在对接zabbix接口的时候,使用 Log::info() 会让日志全部记录在 storage/logs/larav ...

随机推荐

  1. TDD原则

    TDD 介绍 测试驱动开发,或者叫 TDD,是一个敏捷方法,通过确保在代码是先前手动编写测试用 例,用测试来驱动开发,从而翻转开发生命周期(它不只是作为一种校验工具). TDD 的原则很简单的: 只有 ...

  2. smartcrop.js智能图片裁剪库

    今天将为大家介绍一款近期github上很不错的开源库 – smartcrop.js.它是一款图片处理的智能裁剪库.在很多项目开发中,经常会遇见上传图片的场景,它可能是用户照片信息,也可能是商品图片等. ...

  3. Netty5使用自签证书实现SSL安全连接

    这次使用的Netty是最新的5.0 Alpha2版本,下载地址是:http://dl.bintray.com/netty/downloads/netty-5.0.0.Alpha2.tar.bz2,发布 ...

  4. 在春意盎然的季节里初识GIT

    Git 与 SVN 区别 GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等. 如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征. ...

  5. [Java面经] 关于面试的二三事.

    今天终于闲下来了, 那么也好总结下这几天面试的经历.四天的时间一共面了七家, 有一家是自己推迟了没有去.声明:如若转载请注明出处:http://www.cnblogs.com/wang-meng/p/ ...

  6. 无需编译、快速生成 Vue 风格的文档网站

    无需编译.快速生成 Vue 风格的文档网站 https://docsify.js.org/#/#coverpage https://github.com/QingWei-Li/docsify/

  7. 【CSS3动画】transform对文字及图片的旋转、缩放、倾斜和移动

    前言:之前我有写过CSS3的transform这一这特性,对于它的用法,还不是很透彻,今天补充补充,呵呵 你懂的,小司机准备开车了. a)再提一提transform的四个属性 ①旋转--->ro ...

  8. 在 CSS 预编译器之后:PostCSS

    提到css预编译器(css preprocessor),你可能想到Sass.Less以及Stylus.而本文要介绍的PostCSS,正是一个这样的工具:css预编译器可以做到的事,它同样可以做到. “ ...

  9. 纪录我的iOS学习之路

    学习资料的网址 田伟宇(Casa Taloyum)有几篇介绍iOS架构的文章,一级棒!原博客链接. iOS应用架构谈 开篇 iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 网络层设计方 ...

  10. 初识AngularJS 之 HelloWorld和数据绑定

    1.Hello World 我用的开发工具是   atom   ,大家有需要的话可以找我要安装包嘻嘻 第一步: 写入以下代码: <!DOCTYPE html> <html ng-ap ...