最近監控線上日誌,網站是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相應的方法。