use htmlhelpter in cshtml
This commit is contained in:
Родитель
058e283c42
Коммит
c08d7b3182
|
@ -1,8 +1,10 @@
|
|||
|
||||
namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
||||
namespace Microsoft.ApplicationInsights.AspNet
|
||||
{
|
||||
using System;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This class helps to inject Application Insights JavaScript snippet into applicaiton code.
|
||||
|
@ -13,7 +15,7 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
|||
/// @Html.Raw(ApplicationInsightsJavaScript.Write())
|
||||
/// </head>
|
||||
/// </summary>
|
||||
public class ApplicationInsightsJavaScript : IJavaScriptSnippet
|
||||
public class JavaScriptSnippet
|
||||
{
|
||||
/// <summary>
|
||||
/// Prefix of the code snippet. Use it if you need to initialize additional properties in javascript configuration.
|
||||
|
@ -41,7 +43,7 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
|||
|
||||
private TelemetryConfiguration telemetryConfiguration;
|
||||
|
||||
public ApplicationInsightsJavaScript(TelemetryConfiguration telemetryConfiguration)
|
||||
public JavaScriptSnippet(TelemetryConfiguration telemetryConfiguration)
|
||||
{
|
||||
this.telemetryConfiguration = telemetryConfiguration;
|
||||
}
|
||||
|
@ -50,7 +52,9 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
|||
/// Returns code snippet with instrumentation key initialized from TelemetryConfiguration.
|
||||
/// </summary>
|
||||
/// <returns>JavaScript code snippet with instrumentation key or empty if instrumentation key was not set for the applicaiton.</returns>
|
||||
public string Write()
|
||||
public string FullScript
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey))
|
||||
{
|
||||
|
@ -64,4 +68,5 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<IJavaScriptSnippet, ApplicationInsightsJavaScript>();
|
||||
|
||||
services.AddScoped<TelemetryClient>();
|
||||
|
||||
services.AddScoped<RequestTelemetry>((svcs) => {
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
namespace Microsoft.AspNet.Mvc.Rendering
|
||||
{
|
||||
using Microsoft.ApplicationInsights.AspNet;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
|
||||
public static class ApplicationInsightsJavaScriptExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension method to inject Application Insights JavaScript snippet into cshml files.
|
||||
/// </summary>
|
||||
/// <param name="helper">Html helper object to align with razor code style.</param>
|
||||
/// <param name="configuration">Telemetry configuraiton to initialize snippet.</param>
|
||||
/// <returns>JavaScript snippt to insert into html page.</returns>
|
||||
public static HtmlString ApplicationInsightsJavaScript(this IHtmlHelper helper, TelemetryConfiguration configuration)
|
||||
{
|
||||
JavaScriptSnippet snippet = new JavaScriptSnippet(configuration);
|
||||
return new HtmlString(snippet.FullScript);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
|
||||
namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
||||
{
|
||||
public interface IJavaScriptSnippet
|
||||
{
|
||||
string Write();
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
@inject IOptions<AppSettings> AppSettings
|
||||
@inject Microsoft.ApplicationInsights.AspNet.JavaScript.IJavaScriptSnippet ApplicationInsightsJavaScript
|
||||
@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
@ -21,7 +21,7 @@
|
|||
asp-fallback-test-class="carousel-caption" asp-fallback-test-property="display" asp-fallback-test-value="none" />
|
||||
<link rel="stylesheet" href="~/css/site.css" />
|
||||
</environment>
|
||||
@Html.Raw(ApplicationInsightsJavaScript.Write())
|
||||
@Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
|
|
Загрузка…
Ссылка в новой задаче