记处理线上记录垃圾日志 The view 'Error' or its master was not found
最近监控线上日志,网站是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的更多相关文章
- 处理线上CPU负载过高的故障现象
如何处理线上CPU100%的故障现象 处理流程: 1.登陆线上机器top命令,查看耗费cpu的进程号,举例来说发现进程24008持续耗费资源 2.top -Hp 24008去查看持续耗费cpu的线程号 ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中
Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中 在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- DB2不记录事务日志
1. DB2大数据处理不记录事务日志步骤: 建表需要添加属性“NOT LOGGED INITIALLY” 在大批量更改操作的同一个事务开始时执行:“ALTER TABLE tabname ACTI ...
- 记录php日志
1.记录PHP错误日志 display_errors与log_errors的区别 display_errors 错误回显,一般常用于开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴 ...
- StackTrace堆栈跟踪记录详细日志
使用StackTrace堆栈跟踪记录详细日志(可获取行号) 2014-04-25 22:30 by 螺丝钉想要螺丝帽, 350 阅读, 3 评论, 收藏, 编辑 上一篇我们提到使用.NET自带的Tra ...
- C#中四步轻松使用log4net记录本地日志
在这里,记录我在项目中使用log4net记录本地日志的步骤.在不会之前感觉很难,很神秘,一旦会了之后其实没那么难.其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验. 第一步:首先从 ...
- Laravel技巧之记录多日志
相信每个小伙伴在使用laravel的时候都会记录日志.查看日志.那么问题来了,比如我在对接zabbix接口的时候,使用 Log::info() 会让日志全部记录在 storage/logs/larav ...
随机推荐
- transient关键字的用法
本篇博客转自 一直在路上 Java transient关键字使用小记 1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,Java ...
- iOS开发系列--网络开发
概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...
- CSharpGL(28)得到高精度可定制字形贴图的极简方法
CSharpGL(28)得到高精度可定制字形贴图的极简方法 回顾 以前我用SharpFont实现了解析TTF文件从而获取字形贴图的功能,并最终实现了用OpenGL渲染文字. 使用SharpFont,美 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- CocoaPods被卡住:Updating local specs repositories
使用CocoaPods被卡住:Updating local specs repositories 使用 pod install --verbose --no-repo-update
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- WCF学习之旅—第三个示例之三(二十九)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) 在上一篇文章中我们创建了实体对象与接口协定,在这一篇文章中我们来学习如何创建WCF的服务端代码.具体步骤见下面. ...
- 微软Ignite大会我的Session(SQL Server 2014 升级面面谈)PPT分享
我在首届微软技术大会的Session分享了一个关于SQL Server升级的主题,现在将PPT分享出来. 您可以点击这里下载PPT. 也非常感谢微软中国邀请我进行这次分享.
- !+"\v1" 用来“判断浏览器类型”还是用来“IE判断版本”的问题!
这种写法是利用各浏览器对转义字符"\v"的理解不同来判断浏览器类型.在IE中,"\v"没有转义,得到的结果为"v".而在其他浏览器中&quo ...
- JavaScript中函数函数的定义与变量的声明<基础知识一>
1.JavaScript中函数的三种构造方式 a.function createFun(){ } b.var createFun=function (){ } c.var createFun=new ...