0

我的 .net MVC 代码中有一个围绕异常的过滤器。它会愉快地记录异常,但几乎总是记录同一异常的变体。

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper'. (Exception of type 'System.Web.HttpUnhandledException' was thrown.)
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.Mvc.ViewPage.ProcessRequest(HttpContext context)
   at ASP.views_metric_details_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\25795fa7\3c9cc227\App_Web_0hhowapn.1.cs:line 0
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass1.b__0()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass4.b__3()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap(Action action)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.ProcessRequest(HttpContext context)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)

这是我的过滤器:

public void OnException(ExceptionContext context)
    {
        // http://blog.tonysneed.com/2011/10/09/using-nlog-with-dependency-injection/

        var exception = context.Exception;
        string assemblyProp = string.Empty;
        string classProp = string.Empty;
        string methodProp = string.Empty;
        string messageProp = string.Empty;
        string innerMessageProp = string.Empty;

        if (exception != null)
        {
            assemblyProp = exception.Source;
            classProp = exception.TargetSite.DeclaringType.FullName;
            methodProp = exception.TargetSite.Name;
            messageProp = exception.Message;
            object[] parameters = new object[1];

            LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, null, context.Exception.Message, parameters, context.Exception);

            if (exception.InnerException != null)
            {
                innerMessageProp = exception.InnerException.Message;
                assemblyProp = exception.InnerException.Source;
                classProp = exception.InnerException.TargetSite.DeclaringType.FullName;
                methodProp = exception.InnerException.TargetSite.Name;

                logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, null, exception.InnerException.Message, parameters, exception.InnerException);
            }

            logEvent.Properties["error-source"] = assemblyProp;
            logEvent.Properties["error-class"] = classProp;
            logEvent.Properties["error-method"] = methodProp;
            logEvent.Properties["error-message"] = messageProp;
            logEvent.Properties["inner-error-message"] = innerMessageProp;

            Dictionary<String, object> parent = new Dictionary<string, object>();

            Dictionary<String, String> routeData = new Dictionary<string, string>();

            foreach (string key in context.RouteData.Values.Keys)
            {
                try
                {
                    routeData[key] = context.RouteData.Values[key].ToString();
                }
                catch (System.InvalidCastException)
                {
                    routeData[key] = context.RouteData.Values[key].GetType().ToString();
                }
            }

            parent["route_data"] = routeData;

            Dictionary<String, String> server = new Dictionary<string, string>();
            foreach (string key in HttpContext.Current.Request.ServerVariables.AllKeys)
            {
                if (HttpContext.Current.Request.ServerVariables[key] != null && key != "AUTH_PASSWORD")
                {
                    try
                    {
                        var x = HttpContext.Current.Request.ServerVariables[key].ToString();
                        server[key] = x;
                    }
                    catch (System.InvalidCastException) { }
                }
            }

            parent["server"] = server;

            var cookies = new Dictionary<string, string>();

            foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
            {
                if (HttpContext.Current.Request.Cookies[key].Value != null && key != "AUTH_PASSWORD")
                {
                    try
                    {
                        var x = HttpContext.Current.Request.Cookies[key].Value.ToString();
                        cookies[key] = x;
                    }
                    catch (System.InvalidCastException) { }
                }

            }
            parent["cookies"] = cookies;
            logEvent.Properties["variables"] = JsonConvert.SerializeObject(parent);

            _logger.Log(logEvent);

            HttpContext.Current.ClearError();
            HttpContext.Current.Response.TrySkipIisCustomErrors = true;

        }
        else
        {
            _logger.Error(context.Exception);
        }
    }

我不确定如何从中获得更有用的东西。

4

1 回答 1

0

创建一个基本控制器并使用基本控制器派生所有控制器。在基本控制器下定义以下代码

protected override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception == null)
            return;

       .........................
       .........................
        base.OnException(filterContext);
    }
于 2013-04-04T07:22:07.760 回答