I just installed Application Insights into my ASP.NET MVC application. It's actually an Umbraco website, and the registration is slightly different but the result should be the same.
When installing the package, it added some code for me to register a new Exception Action Filter globally called 'AiHandleErrorAttribute'.
I'm registering it the Umbraco way using an event handler:
public class RegisterAIEventHandler : ApplicationEventHandler
{
protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationInitialized(umbracoApplication, applicationContext);
GlobalFilters.Filters.Add(new ErrorHandler.AiHandleErrorAttribute());
}
}
And this is the Action Filter code:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AiHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
{
//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
var ai = new TelemetryClient();
ai.TrackException(filterContext.Exception);
}
}
base.OnException(filterContext);
}
}
Whenever an exception is thrown, the Action Filter isn't triggered but the Exception is still recorded correctly in Application Insights.
When I inspect all Global Action Filters, I noticed there's another Action Filter registered by Application Insights automatically.
So I have few questions:
Is the AppInsights Action Filter that automatically got registered preventing my filter from being run?- Do I even need the custom Action Filter that AppInsights generated for me, since the exceptions seem to be captured by the other Action Filter added by AppInsights?
- Should I remove the Action Filter added by AppInsights or the custom action filter?
Edit: The reason the custom Action Filter isn't triggered is because the exception I was purposely setting off was thrown before I got into the Controller Pipeline. Now that I'm triggering an exception inside of the controller, it actually gets called.
Though, I still question why there's an Action filter added automatically, and if I should add the custom AiHandleErrorAttribute as well.