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:
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- // Added this when converting to MVC 3
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
- }
With the Application_Start calling the RegisterGlobalFilters…
- // Added this when converting to MVC 3
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- if (ReferenceEquals(filters, null))
- {
- throw new ArgumentNullException("filters");
- }
- filters.Add(new HandleErrorAttribute());
- }
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:
- public class HandleErrorWithInsightAttribute : HandleErrorAttribute
- {
- public HandleErrorWithInsightAttribute()
- : this(new InsightManagerWrapper())
- { }
- [CLSCompliant(false)]
- public HandleErrorWithInsightAttribute(IInsightManagerBase insightManager)
- : base()
- {
- Manager = insightManager;
- }
- private IInsightManagerBase Manager { get; set; }
- public override void 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:
- [HandleErrorWithInsight]
- public class AccountController : 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
Leave a Reply