From 5f0325d06bd20fa9425b80f19ab1867450d48247 Mon Sep 17 00:00:00 2001 From: Sergey Kanzhelev Date: Mon, 30 Mar 2015 11:48:40 -0700 Subject: [PATCH] test that empty instrumentation key will not break customer applicaiton --- .../ApplicationInsightsExtensions.cs | 19 +- .../ApplicationInsightsExtensionsTests.cs | 340 ++++++++++++++++++ .../content/config.json | 5 + .../project.json | 8 +- 4 files changed, 368 insertions(+), 4 deletions(-) create mode 100644 test/ApplicationInsights.AspNet.Tests/ApplicationInsightsExtensionsTests.cs create mode 100644 test/ApplicationInsights.AspNet.Tests/content/config.json diff --git a/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs b/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs index ff510a7..7bb3d12 100644 --- a/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs +++ b/src/ApplicationInsights.AspNet/ApplicationInsightsExtensions.cs @@ -31,10 +31,25 @@ public static void AddApplicationInsightsTelemetry(this IServiceCollection services, IConfiguration config) { - TelemetryConfiguration.Active.InstrumentationKey = config.Get("ApplicationInsights:InstrumentationKey"); + // Do not initialize key if customer has already set it. + if (string.IsNullOrWhiteSpace(TelemetryConfiguration.Active.InstrumentationKey)) + { + // Read from configuration + // Config.json will look like this: + // + // "ApplicationInsights": { + // "InstrumentationKey": "11111111-2222-3333-4444-555555555555" + // } + var instrumentationKey = config.Get("ApplicationInsights:InstrumentationKey"); + if (!string.IsNullOrWhiteSpace(instrumentationKey)) + { + TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey; + } + } services.AddSingleton((svcs) => { TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebClientIpHeaderTelemetryInitializer(svcs)); + TelemetryConfiguration.Active.TelemetryInitializers.Add(new WebUserAgentTelemetryInitializer(svcs)); return new TelemetryClient(); }); @@ -50,8 +65,6 @@ public static HtmlString ApplicationInsightsJavaScriptSnippet(this IHtmlHelper helper, string instrumentationKey) { - //see: https://github.com/aspnet/Mvc/issues/2056 - //var client = (TelemetryClient)helper.ViewContext.HttpContext.ApplicationServices.GetService(typeof(TelemetryClient)); return new HtmlString(@"