记处置线上记录垃圾日志 The view 'Error' or its master was not found

记处理线上记录垃圾日志 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的代码如下:

记处置线上记录垃圾日志 The view 'Error' or its master was not found记处置线上记录垃圾日志 The view 'Error' or its master was not found
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;
        }
    }
}
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相应的方法。