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 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; | |
} | |
} | |
} | |
} |
This code is at least 4 years old, but:
- 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.
- Neither the
tags
variable or theConfigurationManager.AppSettings["InstrumentationTags"]
configuration have effect on theInstrumentationKey
setting or configuration.
Hope it helps...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Carlos, I have a question about TelemetryConfiguration.Active.
if I used this code:
var config = TelemetryConfiguration.Active;
config.InstrumentationKey = AISetting.InstrumentationKey;
Only the first row can be run, and the second row cannot be run. but if I add one row of var setting = AISetting;
var setting = AISetting;
var config = TelemetryConfiguration.Active;
config.InstrumentationKey = AISetting.InstrumentationKey;
i can run successfully. I think var setting=AISetting is a meaningless statement. but if without this statement, I can't run successful.
I look at your code and I see that you're using this
private static string tags = ConfigurationManager.AppSettings["InstrumentationTags"];
may i know if this (private static string tags = ConfigurationManager.AppSettings["InstrumentationTags"];) have effect on:
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
ConfigurationManager.AppSettings["InstrumentationKey"];
Thank you very much in advance.