最近監控線上日志,網站是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() != 500) { 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 = 500; // 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; } } }
坑爹,他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相應的方法。