• February 2011
    M T W T F S S
     123456
    78910111213
    14151617181920
    21222324252627
    28  
  • Latest Posts

  • Latest Comments

  • Archives

MVC 3 Breaks Error Tracking in CodeSmith Insight

I finally have the Crash Reports working again with CodeSmith’s Insight.

The release of MVC 3 provided a new way to deal with errors using the HandleErrorAttribute. The changes to the Global.asax.cs file look something like this:

Application_Start
  1.     protected void Application_Start()
  2.     {
  3.         AreaRegistration.RegisterAllAreas();
  4.  
  5.         // Added this when converting to MVC 3
  6.         RegisterGlobalFilters(GlobalFilters.Filters);
  7.  
  8.         RegisterRoutes(RouteTable.Routes);
  9.     }

With the Application_Start calling the RegisterGlobalFilters…

Code Snippet
  1. // Added this when converting to MVC 3
  2. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  3. {
  4.     if (ReferenceEquals(filters, null))
  5.     {
  6.         throw new ArgumentNullException("filters");
  7.     }
  8.     filters.Add(new HandleErrorAttribute());
  9. }

If you have HandleError attribute and the <customErrors Mode=”On” /> in the system.web section of your web.config, then you want get Crash Reports with CodeSmith Insight. Piecing together information from several posts along with a post of mine about Unit Testing CodeSmith Insight I was able to develop a strategy for getting it working again.

The HandleErrorWithInsightAttribute

To deal with this I implemented the following class:

HandleErrorWithInsight
  1. public class HandleErrorWithInsightAttribute : HandleErrorAttribute
  2. {
  3.  
  4.     public HandleErrorWithInsightAttribute()
  5.         : this(new InsightManagerWrapper())
  6.     { }
  7.  
  8.     [CLSCompliant(false)]
  9.     public HandleErrorWithInsightAttribute(IInsightManagerBase insightManager)
  10.         : base()
  11.     {
  12.         Manager = insightManager;
  13.     }
  14.  
  15.     private IInsightManagerBase Manager { get; set; }
  16.  
  17.     public override void OnException(ExceptionContext filterContext)
  18.     {
  19.         var newCase = Manager.CreateCase(filterContext.Exception);
  20.         Manager.SubmitCase(newCase);
  21.         // When unit testing, it is hard to get base.OnException to
  22.         // work. Setting up the filterContext is hard. For now
  23.         // skip over this exception.
  24.         try
  25.         {
  26.             base.OnException(filterContext);
  27.         }
  28.         catch (NotImplementedException)
  29.         {
  30.  
  31.         }
  32.     }
  33. }

I overrode the OnException method so that I could handle the logging. Look at the Unit Testing CodeSmith Insight post again to see why I use the IInsightManagerBase class. That is for unit testing. This also turned out to be pretty tricky to unit test, but I got most of it tested and now all is working well.

The final note is that on my classes now I decorate them with the new attributes, like this:

New Error Attribute
  1. [HandleErrorWithInsight]
  2. public class AccountController : Controller
  3. {

With these changes I get nicer errors (read no YSOD (Yellow Screen of Death)) but I still will get notified when users uncover issues on the site.

Cheers – Karl

2 Comments  »

  1. Hello,

    We are currently looking into this bug and will keep you updated.

    Thanks
    -Blake Niemyjski

  2. Hello,

    We have been hard at work on a fix for this. Please contact support for access to a nightly client build which fixes this issue.

    Thanks again for reporting this.

    Thanks

    -Blake Niemyjski

RSS feed for comments on this post, TrackBack URI

Leave a Comment