AiHandleErrorAttribute Vs. Built-In auto added Action Filter provided by Application Insights

限于喜欢 提交于 2019-12-11 08:12:29

问题


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:

  1. Is the AppInsights Action Filter that automatically got registered preventing my filter from being run?
  2. 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?
  3. 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.


回答1:


I also just ran into this. My exceptions were being logged twice in AI.

As it turns out, starting with version 2.6 (April 2018) a global filter is automatically added. If you had previously followed the documentation and set it up yourself, everything would now be logged twice.

The global filter that is added looks like this:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class MvcExceptionFilter : HandleErrorAttribute
{
    public const bool IsAutoInjected = true;
    private readonly TelemetryClient telemetryClient = new TelemetryClient();

    public MvcExceptionFilter(TelemetryClient tc) : base()
    {
        telemetryClient = tc;
    }

    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null && filterContext.HttpContext.IsCustomErrorEnabled)
            telemetryClient.TrackException(new ExceptionTelemetry(filterContext.Exception));
        }
    }
}

If you haven't added anything to the AiHandleErrorAttribute that was given in the documentation previously, you can remove it and let the automatically generated one handle everything.

If you do want to use your own version, to have more control over it (for example ignoring certain exceptions), you can disable the automatic exception tracking. Add this to your ApplicationInsights.config:

<Add Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web">  
 <EnableMvcAndWebApiExceptionAutoTracking>false</EnableMvcAndWebApiExceptionAutoTracking>
</Add>

Note that the ExceptionTrackingTelemetryModule element will already exist, you just have to add the setting to it.



来源:https://stackoverflow.com/questions/51548243/aihandleerrorattribute-vs-built-in-auto-added-action-filter-provided-by-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!