biswpo
biswpo

Reputation: 809

Azure Application Insights is marking response code zero as success

Recently we encountered an issue where our servers times out because of huge traffic surge, and those request telemetries were logged into AI as success with response code zero. Is there any way to configure response code zero to be termed as failure. Since request telemetries are captured automatically by AI so we dont have any handle on that

Upvotes: 1

Views: 1967

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

You can do it by using ITelemetryInitializer in your .NET core project.

To be termed as failure when response code is zero, you can set the Success property of request telemetry data as false. The sample code as below(using .NET core 2.2 for this test). And please make sure you're using the latest version of Microsoft.ApplicationInsights.AspNetCore 2.13.1.

Here is the custom 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;
            }
        }
    }
}

then register it in the Startup.cs -> ConfigureServices method:

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

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

    }

After executing the code, in azure portal -> your application insights -> Logs, you can see the Success property of request are made as false:

enter image description here

Upvotes: 2

Related Questions