diff --git a/src/Microsoft.ApplicationInsights.AspNet/JavaScript/ApplicationInsightsJavaScript.cs b/src/Microsoft.ApplicationInsights.AspNet/ApplicationInsightsJavaScript.cs similarity index 73% rename from src/Microsoft.ApplicationInsights.AspNet/JavaScript/ApplicationInsightsJavaScript.cs rename to src/Microsoft.ApplicationInsights.AspNet/ApplicationInsightsJavaScript.cs index da4f916..40602c0 100644 --- a/src/Microsoft.ApplicationInsights.AspNet/JavaScript/ApplicationInsightsJavaScript.cs +++ b/src/Microsoft.ApplicationInsights.AspNet/ApplicationInsightsJavaScript.cs @@ -1,19 +1,15 @@  -namespace Microsoft.ApplicationInsights.AspNet.JavaScript +namespace Microsoft.ApplicationInsights.AspNet { using System; using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.AspNet.Mvc.Rendering; + /// /// This class helps to inject Application Insights JavaScript snippet into applicaiton code. - /// Usage: - /// Inject an instance of the class into your razor code using code: - /// @inject Microsoft.ApplicationInsights.AspNet.JavaScript.IJavaScriptSnippet ApplicationInsightsJavaScript - /// Place this code fragment just before closing "head" tag - /// @Html.Raw(ApplicationInsightsJavaScript.Write()) - /// </head> /// - public class ApplicationInsightsJavaScript : IJavaScriptSnippet + public class JavaScriptSnippet { /// /// Prefix of the code snippet. Use it if you need to initialize additional properties in javascript configuration. @@ -41,7 +37,7 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript private TelemetryConfiguration telemetryConfiguration; - public ApplicationInsightsJavaScript(TelemetryConfiguration telemetryConfiguration) + public JavaScriptSnippet(TelemetryConfiguration telemetryConfiguration) { this.telemetryConfiguration = telemetryConfiguration; } @@ -50,17 +46,20 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript /// Returns code snippet with instrumentation key initialized from TelemetryConfiguration. /// /// JavaScript code snippet with instrumentation key or empty if instrumentation key was not set for the applicaiton. - public string Write() + public string FullScript { - if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey)) + get { - return ScriptPrefix + @"{ + if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey)) + { + return ScriptPrefix + @"{ instrumentationKey: '" + this.telemetryConfiguration.InstrumentationKey + @"' }" + ScriptPostfix; - } - else - { - return string.Empty; + } + else + { + return string.Empty; + } } } } diff --git a/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsExtensions.cs b/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsExtensions.cs index ecfa5c8..2d20c5e 100644 --- a/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsExtensions.cs +++ b/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsExtensions.cs @@ -6,7 +6,6 @@ using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.AspNet; using Microsoft.ApplicationInsights.AspNet.ContextInitializers; - using Microsoft.ApplicationInsights.AspNet.JavaScript; using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.DataContracts; @@ -55,8 +54,6 @@ return telemetryConfiguration; }); - services.AddSingleton(); - services.AddScoped(); services.AddScoped((svcs) => { diff --git a/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsJavaScriptExtensions.cs b/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsJavaScriptExtensions.cs new file mode 100644 index 0000000..d20e933 --- /dev/null +++ b/src/Microsoft.ApplicationInsights.AspNet/Extensions/ApplicationInsightsJavaScriptExtensions.cs @@ -0,0 +1,20 @@ +namespace Microsoft.AspNet.Mvc.Rendering +{ + using Microsoft.ApplicationInsights.AspNet; + using Microsoft.ApplicationInsights.Extensibility; + + public static class ApplicationInsightsJavaScriptExtensions + { + /// + /// Extension method to inject Application Insights JavaScript snippet into cshml files. + /// + /// Html helper object to align with razor code style. + /// Telemetry configuraiton to initialize snippet. + /// JavaScript snippt to insert into html page. + public static HtmlString ApplicationInsightsJavaScript(this IHtmlHelper helper, TelemetryConfiguration configuration) + { + JavaScriptSnippet snippet = new JavaScriptSnippet(configuration); + return new HtmlString(snippet.FullScript); + } + } +} diff --git a/src/Microsoft.ApplicationInsights.AspNet/JavaScript/IApplicationInsightsJavaScript.cs b/src/Microsoft.ApplicationInsights.AspNet/JavaScript/IApplicationInsightsJavaScript.cs deleted file mode 100644 index 8cb5cfc..0000000 --- a/src/Microsoft.ApplicationInsights.AspNet/JavaScript/IApplicationInsightsJavaScript.cs +++ /dev/null @@ -1,8 +0,0 @@ - -namespace Microsoft.ApplicationInsights.AspNet.JavaScript -{ - public interface IJavaScriptSnippet - { - string Write(); - } -} diff --git a/test/Microsoft.ApplicationInsights.AspNet.Tests/JavaScript/ApplicationInsightsJavaScriptTest.cs b/test/Microsoft.ApplicationInsights.AspNet.Tests/JavaScript/ApplicationInsightsJavaScriptTest.cs index c780434..edef370 100644 --- a/test/Microsoft.ApplicationInsights.AspNet.Tests/JavaScript/ApplicationInsightsJavaScriptTest.cs +++ b/test/Microsoft.ApplicationInsights.AspNet.Tests/JavaScript/ApplicationInsightsJavaScriptTest.cs @@ -1,6 +1,6 @@ namespace Microsoft.Framework.DependencyInjection { - using Microsoft.ApplicationInsights.AspNet.JavaScript; + using Microsoft.ApplicationInsights.AspNet; using Microsoft.ApplicationInsights.Extensibility; using Xunit; @@ -10,16 +10,16 @@ public static void SnippetWillBeEmptyWhenInstrumentationKeyIsNotDefined() { var telemetryConfigurationWithNullKey = new TelemetryConfiguration(); - var snippet = new ApplicationInsightsJavaScript(telemetryConfigurationWithNullKey); - Assert.Equal(string.Empty, snippet.Write()); + var snippet = new JavaScriptSnippet(telemetryConfigurationWithNullKey); + Assert.Equal(string.Empty, snippet.FullScript.ToString()); } [Fact] public static void SnippetWillBeEmptyWhenInstrumentationKeyIsEmpty() { var telemetryConfigurationWithEmptyKey = new TelemetryConfiguration { InstrumentationKey = string.Empty }; - var snippet = new ApplicationInsightsJavaScript(telemetryConfigurationWithEmptyKey); - Assert.Equal(string.Empty, snippet.Write()); + var snippet = new JavaScriptSnippet(telemetryConfigurationWithEmptyKey); + Assert.Equal(string.Empty, snippet.FullScript.ToString()); } @@ -28,8 +28,8 @@ { string unittestkey = "unittestkey"; var telemetryConfiguration = new TelemetryConfiguration { InstrumentationKey = unittestkey }; - var snippet = new ApplicationInsightsJavaScript(telemetryConfiguration); - Assert.Contains("'" + unittestkey + "'", snippet.Write()); + var snippet = new JavaScriptSnippet(telemetryConfiguration); + Assert.Contains("'" + unittestkey + "'", snippet.FullScript.ToString()); } } } diff --git a/test/Mvc6Framework45.FunctionalTests/Views/Shared/_Layout.cshtml b/test/Mvc6Framework45.FunctionalTests/Views/Shared/_Layout.cshtml index 781f14c..02e2647 100644 --- a/test/Mvc6Framework45.FunctionalTests/Views/Shared/_Layout.cshtml +++ b/test/Mvc6Framework45.FunctionalTests/Views/Shared/_Layout.cshtml @@ -1,5 +1,5 @@ @inject IOptions AppSettings -@inject Microsoft.ApplicationInsights.AspNet.JavaScript.IJavaScriptSnippet ApplicationInsightsJavaScript +@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration @@ -21,7 +21,7 @@ asp-fallback-test-class="carousel-caption" asp-fallback-test-property="display" asp-fallback-test-value="none" /> - @Html.Raw(ApplicationInsightsJavaScript.Write()) + @Html.ApplicationInsightsJavaScript(TelemetryConfiguration)