Merge pull request #70 from Microsoft/sergkanz/htmlhelpter
use htmlhelpter in cshtml
This commit is contained in:
Коммит
cf631db1cb
|
@ -1,19 +1,15 @@
|
||||||
|
|
||||||
namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
namespace Microsoft.ApplicationInsights.AspNet
|
||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class helps to inject Application Insights JavaScript snippet into applicaiton code.
|
/// 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>
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ApplicationInsightsJavaScript : IJavaScriptSnippet
|
public class JavaScriptSnippet
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prefix of the code snippet. Use it if you need to initialize additional properties in javascript configuration.
|
/// 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;
|
private TelemetryConfiguration telemetryConfiguration;
|
||||||
|
|
||||||
public ApplicationInsightsJavaScript(TelemetryConfiguration telemetryConfiguration)
|
public JavaScriptSnippet(TelemetryConfiguration telemetryConfiguration)
|
||||||
{
|
{
|
||||||
this.telemetryConfiguration = telemetryConfiguration;
|
this.telemetryConfiguration = telemetryConfiguration;
|
||||||
}
|
}
|
||||||
|
@ -50,17 +46,20 @@ namespace Microsoft.ApplicationInsights.AspNet.JavaScript
|
||||||
/// Returns code snippet with instrumentation key initialized from TelemetryConfiguration.
|
/// Returns code snippet with instrumentation key initialized from TelemetryConfiguration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>JavaScript code snippet with instrumentation key or empty if instrumentation key was not set for the applicaiton.</returns>
|
/// <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
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey))
|
get
|
||||||
{
|
{
|
||||||
return ScriptPrefix + @"{
|
if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey))
|
||||||
|
{
|
||||||
|
return ScriptPrefix + @"{
|
||||||
instrumentationKey: '" + this.telemetryConfiguration.InstrumentationKey + @"'
|
instrumentationKey: '" + this.telemetryConfiguration.InstrumentationKey + @"'
|
||||||
}" + ScriptPostfix;
|
}" + ScriptPostfix;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,6 @@
|
||||||
using Microsoft.ApplicationInsights;
|
using Microsoft.ApplicationInsights;
|
||||||
using Microsoft.ApplicationInsights.AspNet;
|
using Microsoft.ApplicationInsights.AspNet;
|
||||||
using Microsoft.ApplicationInsights.AspNet.ContextInitializers;
|
using Microsoft.ApplicationInsights.AspNet.ContextInitializers;
|
||||||
using Microsoft.ApplicationInsights.AspNet.JavaScript;
|
|
||||||
using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers;
|
using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers;
|
||||||
using Microsoft.ApplicationInsights.Channel;
|
using Microsoft.ApplicationInsights.Channel;
|
||||||
using Microsoft.ApplicationInsights.DataContracts;
|
using Microsoft.ApplicationInsights.DataContracts;
|
||||||
|
@ -55,8 +54,6 @@
|
||||||
return telemetryConfiguration;
|
return telemetryConfiguration;
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddSingleton<IJavaScriptSnippet, ApplicationInsightsJavaScript>();
|
|
||||||
|
|
||||||
services.AddScoped<TelemetryClient>();
|
services.AddScoped<TelemetryClient>();
|
||||||
|
|
||||||
services.AddScoped<RequestTelemetry>((svcs) => {
|
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
|
namespace Microsoft.Framework.DependencyInjection
|
||||||
{
|
{
|
||||||
using Microsoft.ApplicationInsights.AspNet.JavaScript;
|
using Microsoft.ApplicationInsights.AspNet;
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
@ -10,16 +10,16 @@
|
||||||
public static void SnippetWillBeEmptyWhenInstrumentationKeyIsNotDefined()
|
public static void SnippetWillBeEmptyWhenInstrumentationKeyIsNotDefined()
|
||||||
{
|
{
|
||||||
var telemetryConfigurationWithNullKey = new TelemetryConfiguration();
|
var telemetryConfigurationWithNullKey = new TelemetryConfiguration();
|
||||||
var snippet = new ApplicationInsightsJavaScript(telemetryConfigurationWithNullKey);
|
var snippet = new JavaScriptSnippet(telemetryConfigurationWithNullKey);
|
||||||
Assert.Equal(string.Empty, snippet.Write());
|
Assert.Equal(string.Empty, snippet.FullScript.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void SnippetWillBeEmptyWhenInstrumentationKeyIsEmpty()
|
public static void SnippetWillBeEmptyWhenInstrumentationKeyIsEmpty()
|
||||||
{
|
{
|
||||||
var telemetryConfigurationWithEmptyKey = new TelemetryConfiguration { InstrumentationKey = string.Empty };
|
var telemetryConfigurationWithEmptyKey = new TelemetryConfiguration { InstrumentationKey = string.Empty };
|
||||||
var snippet = new ApplicationInsightsJavaScript(telemetryConfigurationWithEmptyKey);
|
var snippet = new JavaScriptSnippet(telemetryConfigurationWithEmptyKey);
|
||||||
Assert.Equal(string.Empty, snippet.Write());
|
Assert.Equal(string.Empty, snippet.FullScript.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,8 +28,8 @@
|
||||||
{
|
{
|
||||||
string unittestkey = "unittestkey";
|
string unittestkey = "unittestkey";
|
||||||
var telemetryConfiguration = new TelemetryConfiguration { InstrumentationKey = unittestkey };
|
var telemetryConfiguration = new TelemetryConfiguration { InstrumentationKey = unittestkey };
|
||||||
var snippet = new ApplicationInsightsJavaScript(telemetryConfiguration);
|
var snippet = new JavaScriptSnippet(telemetryConfiguration);
|
||||||
Assert.Contains("'" + unittestkey + "'", snippet.Write());
|
Assert.Contains("'" + unittestkey + "'", snippet.FullScript.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
@inject IOptions<AppSettings> AppSettings
|
@inject IOptions<AppSettings> AppSettings
|
||||||
@inject Microsoft.ApplicationInsights.AspNet.JavaScript.IJavaScriptSnippet ApplicationInsightsJavaScript
|
@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
asp-fallback-test-class="carousel-caption" asp-fallback-test-property="display" asp-fallback-test-value="none" />
|
asp-fallback-test-class="carousel-caption" asp-fallback-test-property="display" asp-fallback-test-value="none" />
|
||||||
<link rel="stylesheet" href="~/css/site.css" />
|
<link rel="stylesheet" href="~/css/site.css" />
|
||||||
</environment>
|
</environment>
|
||||||
@Html.Raw(ApplicationInsightsJavaScript.Write())
|
@Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||||
|
|
Загрузка…
Ссылка в новой задаче