2013-04-03 112 views
0

我有一個過濾器在我的.net MVC代碼中包裹異常。它很高興地記錄異常,但幾乎總是記錄相同異常的變化。Asp.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); 
     } 
    } 

我不知道如何得到的東西更多有用出於此。

回答

0

創建一個基礎控制器,並派生所有使用基礎控制器的控制器。在基本控制器下定義以下代碼

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

     ......................... 
     ......................... 
     base.OnException(filterContext); 
    }