Skip to content

Instantly share code, notes, and snippets.

@cmendible
Last active October 28, 2020 15:23
Show Gist options
  • Save cmendible/0c333ea93d94ddbbf600 to your computer and use it in GitHub Desktop.
Save cmendible/0c333ea93d94ddbbf600 to your computer and use it in GitHub Desktop.
Application Insights Telemetry Initializer to send the application version and a custom "tags" property
namespace Insights
{
using System.Configuration;
using System.Linq;
using System.Reflection;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
/// <summary>
/// Version TelemetryInitializer
/// </summary>
public class VersionTelemetryInitializer : ITelemetryInitializer
{
private static string version = string.Empty;
private static string tags = ConfigurationManager.AppSettings["InstrumentationTags"];
/// <summary>
/// Initializes properties of the specified
/// <see cref="T:Microsoft.ApplicationInsights.Channel.ITelemetry" /> object.
/// </summary>
/// <param name="telemetry">the telemetry channel</param>
public void Initialize(ITelemetry telemetry)
{
// Application Version
telemetry.Context.Component.Version = VersionTelemetryInitializer.version;
// Environment Tags
telemetry.Context.Properties["tags"] = VersionTelemetryInitializer.tags;
}
/// <summary>
/// Adds the VersionTelemetryInitializer to Microsoft.ApplicationInsights
/// </summary>
public static void Configure(Assembly assembly)
{
VersionTelemetryInitializer.version = assembly.GetName().Version.ToString();
if (ConfigurationManager.AppSettings.AllKeys.Contains("InstrumentationKey") && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["InstrumentationKey"]))
{
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryInitializers
.Add(new VersionTelemetryInitializer());
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
ConfigurationManager.AppSettings["InstrumentationKey"];
}
else
{
TelemetryConfiguration.Active.DisableTelemetry = true;
}
}
}
}
@cmendible
Copy link
Author

This code is at least 4 years old, but:

  1. I would need to to see the code to understand what can be happening there. With the information you provided it doesn't make sense for me either.
  2. Neither the tags variable or the ConfigurationManager.AppSettings["InstrumentationTags"] configuration have effect on the InstrumentationKey setting or configuration.

Hope it helps...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment