AiHandleErrorAttribute与. Application Insights提供的内置自动添加的动作筛选器 [英] AiHandleErrorAttribute Vs. Built-In auto added Action Filter provided by Application Insights

查看:117
本文介绍了AiHandleErrorAttribute与. Application Insights提供的内置自动添加的动作筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将Application Insights安装到我的ASP.NET MVC应用程序中.它实际上是Umbraco网站,注册稍有不同,但结果应该相同.

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.

安装该软件包时,它为我添加了一些代码,以全局注册一个名为"AiHandleErrorAttribute"的新异常操作过滤器.

When installing the package, it added some code for me to register a new Exception Action Filter globally called 'AiHandleErrorAttribute'.

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

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

这是动作过滤器代码:

[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中正确记录该异常.

Whenever an exception is thrown, the Action Filter isn't triggered but the Exception is still recorded correctly in Application Insights.

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

When I inspect all Global Action Filters, I noticed there's another Action Filter registered by Application Insights automatically.

所以我有几个问题:

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


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


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.

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

Though, I still question why there's an Action filter added automatically, and if I should add the custom AiHandleErrorAttribute as well.

推荐答案

我也遇到了这个问题.我的例外情况在AI中记录了两次.

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

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

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.

添加的全局过滤器看起来像这样:

[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添加任何内容,则可以将其删除,并让自动生成的内容处理一切.

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>

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

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

这篇关于AiHandleErrorAttribute与. Application Insights提供的内置自动添加的动作筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆