Dave New
Dave New

Reputation: 40012

Application Insights and failed request response codes

When an exception is unhandled in our web API, a response code 500 (Internal Server Error) will return to the client.

Although Application Insights doesn't record this as a 500, but rather a 200. Successful request is false, but the response code is still wrong.

Application Insights failed request

How can I get the corrrect response codes in my telemetry?

Startup's Configure:

public void Configure(IApplicationBuilder app, IHostingEnvironment environment)
{
    if (!TelemetryConfiguration.Active.DisableTelemetry)
    {
        // Add Application Insights to the beginning of the request pipeline to track HTTP request telemetry.
        app.UseApplicationInsightsRequestTelemetry();

        // Add Application Insights exceptions handling to the request pipeline. Should be
        // configured after all error handling middleware in the request pipeline.
        app.UseApplicationInsightsExceptionTelemetry();
    }

    app.UseRequireHttps(environment.IsLocal());
    app.UseMiddleware<NoCacheMiddleware>();
    app.UseJwtBearerAuthentication(...);
    app.UseCors("CorsPolicy");
    app.UseStaticFiles();
    app.UseCompression();
    app.UseMvc();
}

Upvotes: 2

Views: 14611

Answers (2)

OzBob
OzBob

Reputation: 4520

ApplicationInsights tracks the interaction but does not report an error without the client logging the error as a Custom Event or the Server having Middleware code added (as per @fei-han)

If you want Analytics alerts then the Azure Data Lake query I use is:

let sitename = "localhost:26047/api";
requests
| where timestamp > ago(1d)
| where url contains sitename
| where resultCode contains "40" 
| order by timestamp desc 

For alerts you could create a new 'Rule' from the Analytics blade for about $1.50 a month.

Upvotes: 0

Fei Han
Fei Han

Reputation: 27793

Although Application Insights doesn't record this as a 500, but rather a 200. Successful request is false, but the response code is still wrong.

As far as I know, if application has no error handling middleware Application Insights will report response status code 200 when unhandled exception is thrown. We could find detailed info from this article. I am using the following code in the method Configure and I could get the correct response status code.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (!Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.DisableTelemetry)
    {
       app.UseApplicationInsightsRequestTelemetry();

       app.UseApplicationInsightsExceptionTelemetry();
    }

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseIISPlatformHandler();
    app.UseExceptionHandler(options => {
        options.Run(
        async context =>
        {
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            context.Response.ContentType = "text/html";
            var ex = context.Features.Get<IExceptionHandlerFeature>();
            if (ex != null)
            {
                var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
                await context.Response.WriteAsync(err).ConfigureAwait(false);
            }
        });
    });

    app.UseStaticFiles();

    app.UseMvc();
}

enter image description here

Upvotes: 4

Related Questions