Last active
October 28, 2020 15:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is at least 4 years old, but:
tags
variable or theConfigurationManager.AppSettings["InstrumentationTags"]
configuration have effect on theInstrumentationKey
setting or configuration.Hope it helps...