【问题标题】:Azure Application Insights is marking response code zero as successAzure Application Insights 将响应代码零标记为成功
【发布时间】:2020-06-13 09:16:54
【问题描述】:

最近我们遇到了一个问题,即我们的服务器由于巨大的流量激增而超时,并且这些请求遥测已成功登录到 AI,响应代码为零。有没有办法将响应代码零配置为失败。由于请求遥测是由 AI 自动捕获的,所以我们对此没有任何处理

【问题讨论】:

  • 是.NET项目(.net core or .NET framework),还是其他项目?
  • .net 核心项目

标签: azure asp.net-core azure-application-insights


【解决方案1】:

您可以通过在 .NET 核心项目中使用 ITelemetryInitializer 来实现。

响应码为零时称为失败,可以将请求遥测数据的Success属性设置为false。示例代码如下(本次测试使用 .NET core 2.2)。请确保您使用的是最新版本的Microsoft.ApplicationInsights.AspNetCore 2.13.1.

这是自定义的 ITelemetryInitializer:

public class MyTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is RequestTelemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;

            //you can change the ResponseCode to "0" in your project
            if (requestTelemetry.ResponseCode == "200")
            {
                // set Success property to false
                requestTelemetry.Success = false;
            }
        }
    }
}

然后在Startup.cs->ConfigureServices方法中注册:

    public void ConfigureServices(IServiceCollection services)
    {         
        //your other code

        //here, register the custom ITelemetryInitializer
        services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();

    }

执行代码后,在azure portal -> your applicationinstances -> Logs,可以看到request的Success属性为false:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多