AiHandleErrorAttribute 与。 Application Insights 提供的内置自动添加操作筛选器

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

我刚刚将 Application Insights 安装到我的 ASP.NET MVC 应用程序中。其实是一个Umbraco网站,注册方式略有不同,但结果应该是一样的。

安装包时,它为我添加了一些代码来注册一个全局名为 'AiHandleErrorAttribute' 的新异常操作过滤器。

我正在使用事件处理程序以 Umbraco 方式注册它:

public class RegisterAIEventHandler : ApplicationEventHandler
{
    protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        base.ApplicationInitialized(umbracoApplication, applicationContext);
        GlobalFilters.Filters.Add(new ErrorHandler.AiHandleErrorAttribute());
    }
}

这是动作过滤器代码:

[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);
    }
}

无论何时抛出异常,都不会触发操作筛选器,但 Application Insights 中仍会正确记录异常。

当我检查所有全局操作过滤器时,我注意到 Application Insights 自动注册了另一个操作过滤器。

所以我有几个问题:

  1. 自动注册的 AppInsights 操作过滤器是否阻止了我的过滤器 运行?
  2. 我什至需要 AppInsights 为我生成的自定义操作过滤器,因为异常似乎被 AppInsights 添加的其他操作过滤器捕获了吗?
  3. 我应该删除 AppInsights 添加的操作过滤器还是自定义操作过滤器?

编辑: 自定义动作过滤器未被触发的原因是我故意设置的异常在我进入控制器管道之前被抛出。现在我在控制器内部触发异常,它实际上被调用了。

尽管如此,我仍然质疑为什么会自动添加一个 Action 过滤器,以及我是否应该添加自定义 AiHandleErrorAttribute。

我也刚刚 运行 进入了这个。我的异常在 AI 中被记录了两次。

事实证明,从 2.6 版开始(2018 年 4 月)a global filter is automatically 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));
        }
    }
}

如果您没有向先前文档中提供的 AiHandleErrorAttribute 添加任何内容,您可以将其删除并让自动生成的处理所有内容。

如果您确实想使用自己的版本,以便更好地控制它(例如忽略某些异常),您可以 disable the automatic exception tracking。 将此添加到您的 ApplicationInsights.config:

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

请注意,ExceptionTrackingTelemetryModule 元素已经存在,您只需向其添加设置。