Thursday, June 4, 2020

Application Insights - Stop tracking 404 errors

By default Application Insights will log every 404 error in your web app as an error. I think this is a good default, but what if you don’t want to see these 404 errors?

There are 2 options to solve this:

Telemetry Processor

A telemetry processor gives you direct control over what is included or excluded from the telemetry stream.

We can register our new TelemetryProcessor by using the AddApplicationInsightsTelemetryProcessor extension method on IServiceCollection, as shown below:

Telemetry Initializer

Telemetry initializers allow you to enrich telemetry with additional information and/or to override telemetry properties set by the standard telemetry modules. By default, any request with a response code >= 400 is flagged as failed. But if we want to treat a 404 as a success, we can provide a telemetry initializer that sets the Success property:

We can register the TelemetryInitializer in our Startup.cs:

Advantage of the Telemetry Initializer is that we still log the 404 event but no longer as an error.

More information: https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling