最近監控線上日誌,網站是ASP.NET MVC 開發的,發現很多錯誤日誌都記錄一樣的內容:html
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。mvc
先說下目前我抓取異常的方式:在Global.asax文件中增長ide
protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); //Write Log }
假如在Controller裏面去override OnException方法進行寫日誌則不會出現這樣的問題,可是在Controller裏記日誌可能會使在View裏面出錯的日誌記錄不到。網站
煩了好久,最後不經意發現MVC裏面會默認註冊一個Filter,代碼以下this
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); }
註冊掉能夠解決上述問題,可是我不太服氣,因而在codeplex翻了mvc的源代碼,HandleErrorAttribute的代碼以下:spa
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視圖,看來之後新開發時仍是建立空項目好了。debug
至於爲啥設置了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相應的方法。日誌