Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Application Insights Build Version on all telemetry

Posted on: 2016-12-15

Something very interesting is to know which version was affected by a telemetry. This is good for custom events, traces and very interesting for exceptions. However, adding this information on every calls are redundant and not clean. That is why, Application Insights allows you to add a telemetry initializer.

A telemetry initializer is a a piece of code that is executed when a telemetry starts. There is two steps to make it works. First, create a class that inherit ITelemetryInitializer. Second, register the class to Application Insights.

To accomplish the goal of having the system version in every telemetry, let's create a class that will add in Application Insights' context a property named BuildVersion. I place this class in my website project which allows me to grap the assembly version. Indeed, you need to use the AssemblyInfo.cs file and its versions on every release to have this method to work.

public class AssemblyVersionInitializer : ITelemetryInitializer { 
   public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry) { 
     telemetry.Context.Properties["BuildVersion"] = this.GetType().Assembly.GetName().Version.ToString(); 
  }
} 

The next and final step is to use this class.

 public class MvcApplication : System.Web.HttpApplication { 
   protected void Application_Start() { 
     TelemetryConfiguration.Active.TelemetryInitializers.Add(new AssemblyVersionInitializer()); 
  } 
} 

From there, what ever you add or not custom properties, it will always have also the BuildVersion one. The goal of having this BuildVersion is primary to see difference in your telemetries between version. You can clearly identify if a problem is resolve or is created between version. You can also see if the performance goes worse. However, this only work if you release often since Application Insights retention of the information if very limited with most of the data is restricted to 7 days (or 14 days).