Skip to content

Instantly share code, notes, and snippets.

@PeterOrneholm
Last active October 11, 2015 18:48
Show Gist options
  • Select an option

  • Save PeterOrneholm/79f5bb94029e4d4ed35a to your computer and use it in GitHub Desktop.

Select an option

Save PeterOrneholm/79f5bb94029e4d4ed35a to your computer and use it in GitHub Desktop.
Boilerplate context initializers for Applicaiton Inisghts.
public class ApplicationInsightsContextInit : IContextInitializer
{
private const string DefaultApplicationSection = "SectionDefault";
private readonly Dictionary<string, string> _pathToSection = new Dictionary<string, string>()
{
{ "/section1", "Section1" },
{ "/section2", "Section2" },
};
public string InstrumentationKey { get; private set; }
public string ApplicationEnvironment { get; private set; }
public string ApplicationVersion { get; private set; }
public ApplicationInsightsContextInit(string instrumentationKey, string applicationEnvironment)
{
InstrumentationKey = instrumentationKey;
ApplicationEnvironment = applicationEnvironment;
ApplicationVersion = GetCurrentVersion();
}
public static string GetCurrentVersion()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
public void Initialize(TelemetryContext context)
{
context.InstrumentationKey = InstrumentationKey;
context.Component.Version = ApplicationVersion;
context.Properties["ApplicationEnvironment"] = ApplicationEnvironment;
context.Properties["ApplicationSection"] = GetApplicationSection();
context.Properties["RequestHttpMethod"] = GetCurrentHttpMethod();
}
private string GetCurrentHttpMethod()
{
if (HttpContext.Current == null)
{
return string.Empty;
}
return HttpContext.Current.Request.HttpMethod;
}
private string GetApplicationSection()
{
if (HttpContext.Current == null)
{
return string.Empty;
}
var path = HttpContext.Current.Request.Path;
var section = _pathToSection.FirstOrDefault(x => path.StartsWith(x.Key, StringComparison.OrdinalIgnoreCase)).Value;
if (string.IsNullOrWhiteSpace(section))
{
section = DefaultApplicationSection;
}
return section;
}
}
public class ApplicationInsightsContextTelemtryInit : ApplicationInsightsContextInit, ITelemetryInitializer
{
public ApplicationInsightsContextTelemtryInit(string instrumentationKey, string applicationEnvironment) : base(instrumentationKey, applicationEnvironment)
{
}
public void Initialize(ITelemetry telemetry)
{
base.Initialize(telemetry.Context);
}
}
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
InitApplicationInsights();
}
private static void InitApplicationInsights()
{
var telemetryConfiguration = TelemetryConfiguration.Active;
var appInsightsInstrumentationKey = ConfigurationManager.AppSettings.Get("ApplicationInsights.InstrumentationKey");
var appInsightsEnvironment = ConfigurationManager.AppSettings.Get("ApplicationInsights.Environment");
var isValidKey = !string.IsNullOrWhiteSpace(appInsightsInstrumentationKey);
if (isValidKey)
{
telemetryConfiguration.DisableTelemetry = false;
telemetryConfiguration.ContextInitializers.Add(new ApplicationInsightsContextInit(appInsightsInstrumentationKey, appInsightsEnvironment));
telemetryConfiguration.TelemetryInitializers.Add(new ApplicationInsightsContextTelemtryInit(appInsightsInstrumentationKey, appInsightsEnvironment));
}
else
{
telemetryConfiguration.DisableTelemetry = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment