Application Insights is an extensible Application Performance Management (APM) service for web developers. It provides rich performance monitoring, powerful alerting, and easy-to-consume dashboards to help ensure your applications are available and performing as you expect.
One of the powerful characteristics of Application Insights is that it provides an extensible API where you can customize the behavior of the standard telemetry modules the way you want. This can be done through ITelemetryInitializer, an interface that allows you to implement custom behavior that plugs into Application Insights to initialize the telemetry data before it is sent to the service. If you provide a telemetry initializer, it is called whenever any of the Track*() methods is called.
Usage Examples
Custom Failed Requests
Application Insights for Web collects telemetry about HTTP requests and flags as failed any request with a response code >= 400. If you want the requests with NotFound (404)
response to be treated as a success, you can provide a telemetry initializer that sets the Success property whenever the response is 404.
using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.ApplicationInsights.Extensibility; namespace MyApp.Api.Infrastructure.Telemetry { public class TelemetryNotFoundInitializer : ITelemetryInitializer { public void Initialize(ITelemetry telemetry) { var requestTelemetry = telemetry as RequestTelemetry; if (requestTelemetry == null) return; int code; var parsed = int.TryParse(requestTelemetry.ResponseCode, out code); if (!parsed) return; if (code == 404) { requestTelemetry.Success = true; // Allow us to filter these requests in the portal: requestTelemetry.Context.Properties["Overridden400s"] = "true"; } } } }
To enable a custom telemetry initializer you need to register it with Application Insights at the startup of your application (Startup.cs or Global.asax)
public class Startup { public void Configuration(IAppBuilder app) { // other initialization code // .... // Telemetry Configuration TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryNotFoundInitializer()); } }
Track Custom Headers
Application Insights does not track all the HTTP request and response headers included in the request data. This may be a problem if you have HTTP requests from different mobile devices and you want to track the device’s type (Android, iOS), or the version that the device is running. To track this data again you can create a custom class that implements ITelemetryInitializer and define the headers you want to track.
using System.Collections.Generic; using System.Web; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Extensibility; namespace MyApp.Api.Infrastructure.Telemetry { public class TelemetryHeadersInitializer : ITelemetryInitializer { public List<string> RequestHeaders { get; set; } public List<string> ResponseHeaders { get; set; } public TelemetryHeadersInitializer() { RequestHeaders = new List<string>(); ResponseHeaders = new List<string>(); } public void Initialize(ITelemetry telemetry) { var requestTelemetry = telemetry as RequestTelemetry; // Is this a TrackRequest() ? if (requestTelemetry == null) return; var context = HttpContext.Current; if (context == null) return; foreach (var headerName in RequestHeaders) { var header = context.Request.Headers[headerName]; if (header != null) { telemetry.Context.Properties.Add($"Request-{headerName}", header); } } foreach (var headerName in ResponseHeaders) { var header = context.Response.Headers[headerName]; if (header != null) { telemetry.Context.Properties.Add($"Response-{headerName}", header); } } } } }
You need then to register the `TelemetryHeadersInitializer` in your startup class (Startup.cs, Global.asax) and define which headers you want to track:
public class Startup { public void Configuration(IAppBuilder app) { // other initialization code // .... // Telemetry Configuration TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryHeadersInitializer() { RequestHeaders = { "DeviceName", "DeviceVersion" }, ResponseHeaders = { "Content-Type" } }); } }
Subsequently, if you click on a request in the Azure portal you will see that the custom headers are being tracked and presented as Custom Data:
Good information but there seems to be an error in your code, you create the class as “public class TelemetryHeadersInitializer : ITelemetryInitializer”
but then initialise it using “TelemetryConfiguration.Active.TelemetryInitializers.Add(new HeaderTelemetryInitializer()”
Should it not be the same as the class so it would become “TelemetryConfiguration.Active.TelemetryInitializers.Add(new TelemetryHeadersInitializer()”
Thanks for your feedback, you are right. I updated the post accordingly.
Hello Paris!
Your article was very helpful thanks!
I was wondering If there a similar implementation to log the body the request?
I tried to implement it an the body stream was already disposed when it reached
the TelemetryHeadersInitializer.
I found a suggestion here : https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/686
but it seems like a hack.
Thanks in advance!
I also agree it’s somewhat a hack.
I think there is a method named `EnableRewind()` that can be applied to the `context.request` and give you access to the Request Body, however I ‘ve never measured the performance on that.
Another way would be to move the tracking to the client side using the javascript SDK of Application Insights https://github.com/microsoft/ApplicationInsights-JS and transmit the request body.