Skip to content

Instantly share code, notes, and snippets.

@cmendible
Last active October 28, 2020 15:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
}
}
}
@LQQ677
Copy link

LQQ677 commented Oct 28, 2020

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.

@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