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

publicclassHandleErrorWithInsightAttribute : HandleErrorAttribute

{

public HandleErrorWithInsightAttribute()

: this(newInsightManagerWrapper())

{ }

[CLSCompliant(false)]

public HandleErrorWithInsightAttribute(IInsightManagerBase insightManager)

: base()

{

Manager = insightManager;

}

privateIInsightManagerBase Manager { get; set; }

publicoverridevoid OnException(ExceptionContext filterContext)

{

var newCase = Manager.CreateCase(filterContext.Exception);

Manager.SubmitCase(newCase);

// When unit testing, it is hard to get base.OnException to

// work. Setting up the filterContext is hard. For now

// skip over this exception.

try

{

base.OnException(filterContext);

}

catch (NotImplementedException)

{

}

}

}

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

[HandleErrorWithInsight]

publicclassAccountController : Controller

{

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

This entry was posted on Friday, February 4th, 2011 at 11:23 pm and is filed under Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.