javascript connection string (#1276)
This commit is contained in:
Родитель
2b22cf7505
Коммит
c198df574a
|
@ -55,24 +55,40 @@
|
|||
/// <returns>JavaScript code snippet with instrumentation key or empty if instrumentation key was not set for the application.</returns>
|
||||
public string FullScript
|
||||
{
|
||||
// TODO: NEED TO SUPPORT CONNECTION STRING. DISCUSS WITH JAVASCRIPT SDK TO CONFIRM COMPATIBILITY.
|
||||
|
||||
get
|
||||
{
|
||||
if (!this.telemetryConfiguration.DisableTelemetry &&
|
||||
!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey))
|
||||
if (!this.telemetryConfiguration.DisableTelemetry)
|
||||
{
|
||||
string additionalJS = string.Empty;
|
||||
IIdentity identity = this.httpContextAccessor?.HttpContext?.User?.Identity;
|
||||
if (this.enableAuthSnippet &&
|
||||
identity != null &&
|
||||
identity.IsAuthenticated)
|
||||
// Config JS SDK
|
||||
string insertConfig;
|
||||
if (!string.IsNullOrEmpty(this.telemetryConfiguration.ConnectionString))
|
||||
{
|
||||
string escapedUserName = this.encoder.Encode(identity.Name);
|
||||
additionalJS = string.Format(CultureInfo.InvariantCulture, AuthSnippet, escapedUserName);
|
||||
insertConfig = string.Format(CultureInfo.InvariantCulture, "connectionString: '{0}'", this.telemetryConfiguration.ConnectionString);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(this.telemetryConfiguration.InstrumentationKey))
|
||||
{
|
||||
insertConfig = string.Format(CultureInfo.InvariantCulture, "instrumentationKey: '{0}'", this.telemetryConfiguration.InstrumentationKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return string.Format(CultureInfo.InvariantCulture, Snippet, this.telemetryConfiguration.InstrumentationKey, additionalJS);
|
||||
// Auth Snippet (setAuthenticatedUserContext)
|
||||
string insertAuthUserContext = string.Empty;
|
||||
if (this.enableAuthSnippet)
|
||||
{
|
||||
IIdentity identity = this.httpContextAccessor?.HttpContext?.User?.Identity;
|
||||
if (identity != null && identity.IsAuthenticated)
|
||||
{
|
||||
string escapedUserName = this.encoder.Encode(identity.Name);
|
||||
insertAuthUserContext = string.Format(CultureInfo.InvariantCulture, AuthSnippet, escapedUserName);
|
||||
}
|
||||
}
|
||||
|
||||
// Return full snippet
|
||||
// Developer Note: If you recently updated the snippet and are now getting "FormatException: Input string was not in a correct format." you need to escape all the curly braces; '{' => '{{' and '}' => '}}'.
|
||||
return string.Format(CultureInfo.InvariantCulture, Snippet, insertConfig, insertAuthUserContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -123,14 +123,14 @@
|
|||
<data name="JavaScriptSnippet" xml:space="preserve">
|
||||
<value><script type="text/javascript">
|
||||
|
||||
var appInsights=window.appInsights||function(a){{
|
||||
function b(a){{c[a]=function(){{var b=arguments;c.queue.push(function(){{c[a].apply(c,b)}})}}}}var c={{config:a}},d=document,e=window;setTimeout(function(){{var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)}});try{{c.cookie=d.cookie}}catch(a){{}}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){{f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){{var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}}}return c
|
||||
}}({{
|
||||
instrumentationKey: '{0}'
|
||||
}});
|
||||
var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){{function n(e){{t[e]=function(){{var n=arguments;t.queue.push(function(){{t[e].apply(t,n)}})}}}}var t={{config:e}};t.initialize=!0;var i=document,a=window;setTimeout(function(){{var n=i.createElement("script");n.src=e.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js",i.getElementsByTagName("script")[0].parentNode.appendChild(n)}});try{{t.cookie=i.cookie}}catch(e){{}}t.queue=[],t.version=2;for(var r=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];r.length;)n("track"+r.pop());n("startTrackPage"),n("stopTrackPage");var s="Track"+r[0];if(n("start"+s),n("stop"+s),n("addTelemetryInitializer"),n("setAuthenticatedUserContext"),n("clearAuthenticatedUserContext"),n("flush"),!(!0===e.disableExceptionTracking||e.extensionConfig&&e.extensionConfig.ApplicationInsightsAnalytics&&!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){{n("_"+(r="onerror"));var o=a[r];a[r]=function(e,n,i,a,s){{var c=o&&o(e,n,i,a,s);return!0!==c&&t["_"+r]({{message:e,url:n,lineNumber:i,columnNumber:a,error:s}}),c}},e.autoExceptionInstrumented=!0}}return t}}(
|
||||
{{
|
||||
{0}
|
||||
}});
|
||||
|
||||
window.appInsights=appInsights,appInsights.queue&&0===appInsights.queue.length&&appInsights.trackPageView();
|
||||
{1}
|
||||
</script></value>
|
||||
window[aiName]=aisdk,aisdk.queue&&0===aisdk.queue.length&&aisdk.trackPageView({{}});
|
||||
{1}
|
||||
|
||||
</script></value>
|
||||
</data>
|
||||
</root>
|
|
@ -43,6 +43,16 @@
|
|||
Assert.Equal(string.Empty, snippet.FullScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Trait("Trait", "ConnectionString")]
|
||||
public static void SnippetWillIncludeConnectionStringAsSubstring()
|
||||
{
|
||||
string testConnString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";
|
||||
var telemetryConfiguration = new TelemetryConfiguration { ConnectionString = testConnString };
|
||||
var snippet = new JavaScriptSnippet(telemetryConfiguration, GetOptions(false), null, encoder);
|
||||
Assert.Contains("connectionString: '" + testConnString + "'", snippet.FullScript);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public static void SnippetWillIncludeInstrumentationKeyAsSubstring()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче