如何在 Azure Application Insights 中抑制 HTTP 错误 440

How can I suppress an http error 440 in Azure Application Insights

Azure Application Insights 是一个很棒的工具,但我们遇到了一些虚假错误。具体来说,当用户在我们的 Web 应用程序上超时时,该应用程序会抛出一个 http 440 错误(我猜这是一个 MS 特定代码),这是会话已过期。这是一种误报,我不想跟踪这些,也不想从他们那里得到警报。

有没有办法在 Application Insights 中抑制这种情况,或者我必须在代码中做些什么才能做到这一点?

如果无法抑制它们,我想我可以设置一个警报,如果我可以从那里过滤掉 440s。

你可以利用ITelemetryProcessor过滤掉http错误440:

在你的 web 项目中,添加一个 class 比如 MyErrorFilter:

    public class MyErrorFilter: ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }

        public MyErrorFilter(ITelemetryProcessor next)
        {
           this.Next = next;
        }

        public void Process(ITelemetry item)
        {
            var request = item as RequestTelemetry;

            if (request != null &&
            request.ResponseCode.Equals("440", StringComparison.OrdinalIgnoreCase))
            {
                // To filter out an item, just terminate the chain:
                return;
            }
            // Send everything else:
            this.Next.Process(item);
        }
    }

如果是.net framework web项目,在ApplicationInsights.config文件中,添加这个(类型是assembly_name.class_name):

<TelemetryProcessors>

  <Add Type="WebApplication9.MyErrorFilter, WebApplication9"></Add>
</TelemetryProcessors>

如果是.net core web项目,安装或更新Microsoft.ApplicationInsights.AspNetCore到2.7.1,然后在Startup.cs -> ConfigureServices方法中,添加下面一行代码:

services.AddApplicationInsightsTelemetry();
services.AddApplicationInsightsTelemetryProcessor<MyErrorFilter>();