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

标签 asp.net .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());
    }
}

这是 Action 过滤器代码:

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

无论何时抛出异常,都不会触发 Action Filter,但 Application Insights 中仍会正确记录异常。

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

所以我有几个问题:

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

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

不过,我仍然质疑为什么会自动添加一个 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 元素已经存在,您只需向其中添加设置即可。

关于asp.net - AiHandleErrorAttribute 与。 Application Insights 提供的内置自动添加操作筛选器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51548243/

相关文章:

asp.net - .skin和.css来自asp.net

c# - ASP.NET : I wanted to know how to insert '\n' on the label

c# - Paypal Adaptive - 支付 API

c# - 如果文件存在于 asp.net mvc 中,则返回链接

asp.net-mvc - 使用 Azure Cmdlet 将 ASP.Net MVC 站点(在 Visual Studio 中创建)发布到 Azure

asp.net - 在统一配置部分与通用生命周期管理器一起工作

c# - List<> 和 IEnumerable<> 开放类型的关系

.net - System.ServiceProcess.ServiceController.Stop() 是同步还是异步?

c# - 属性网格 : How to set Display name label of each property to the right of grid?

c# - 一种将一个列表转换为另一个列表的方法(通过多态性、子类和父类)