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

Swi*_*ger 1 .net asp.net asp.net-mvc umbraco azure-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());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是动作过滤器代码:

[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);
    }
}
Run Code Online (Sandbox Code Playgroud)

每当引发异常时,都不会触发操作筛选器,但仍会在Application Insights中正确记录该异常。

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

所以我有几个问题:

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

编辑:自定义动作筛选器未触发的原因是,在进入控制器管道之前,我故意引发了异常。现在,我在控制器内部触发了一个异常,实际上它已被调用。

虽然,我仍然质疑为什么要自动添加一个动作过滤器,以及是否也应该添加自定义AiHandleErrorAttribute。

小智 5

我也碰到了这个。我的例外情况在AI中记录了两次。

事实证明,从2.6版(2018年4月)开始,会自动添加一个全局过滤器。如果您以前已按照文档进行操作并自行设置,则现在所有内容都会记录两次。

添加的全局过滤器如下所示

[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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

如果您确实想使用自己的版本,以对其进行更多控制(例如忽略某些异常),则可以禁用自动异常跟踪。将此添加到您的ApplicationInsights.config中:

<Add Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web">  
 <EnableMvcAndWebApiExceptionAutoTracking>false</EnableMvcAndWebApiExceptionAutoTracking>
</Add>
Run Code Online (Sandbox Code Playgroud)

请注意,ExceptionTrackingTelemetryModule元素将已经存在,只需将设置添加到其中即可。