Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions WEB/Src/Web/Web/ApplicationInsightsHttpModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Web;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Web.Extensions;
using Microsoft.ApplicationInsights.Web.Implementation;

/// <summary>
/// Platform agnostic module for web application instrumentation.
Expand Down Expand Up @@ -50,6 +51,19 @@ public void Init(HttpApplication context)
System.Diagnostics.Debug.WriteLine("Performing first-time initialization");

sharedTelemetryConfiguration = TelemetryConfiguration.CreateDefault();

// Read connection string from applicationinsights.config
string connectionString = ApplicationInsightsConfigurationReader.GetConnectionString();
if (!string.IsNullOrEmpty(connectionString))
{
sharedTelemetryConfiguration.ConnectionString = connectionString;
// TODO: Log - $"ConnectionString loaded from config: {connectionString}"
}
else
{
// TODO: Log - "No ConnectionString found in applicationinsights.config"
}

sharedTelemetryConfiguration.ConfigureOpenTelemetryBuilder(
builder => builder.UseApplicationInsightsAspNetTelemetry());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
namespace Microsoft.ApplicationInsights.Web.Implementation
{
using System;
using System.IO;
using System.Xml.Linq;

/// <summary>
/// Reads connection string from ApplicationInsights.config file.
/// </summary>
internal static class ApplicationInsightsConfigurationReader
{
private const string ConfigFileName = "ApplicationInsights.config";
private static readonly XNamespace XmlNamespace = "http://schemas.microsoft.com/ApplicationInsights/2013/Settings";

/// <summary>
/// Reads the connection string from ApplicationInsights.config file.
/// </summary>
/// <returns>The connection string if found; otherwise, null.</returns>
public static string GetConnectionString()
{
string configPath = GetConfigFilePath();

if (configPath == null)
{
return null;
}

try
{
// Ensure config file actually exists
if (!File.Exists(configPath))
{
WebEventSource.Log.ApplicationInsightsConfigNotFound(configPath);
return null;
}

return ReadConnectionStringFromConfig(configPath);
}
catch (FileNotFoundException)
{
WebEventSource.Log.ApplicationInsightsConfigNotFound(configPath);
}
catch (DirectoryNotFoundException)
{
WebEventSource.Log.ApplicationInsightsConfigNotFound(configPath);
}
catch (IOException)
{
WebEventSource.Log.ApplicationInsightsConfigNotFound(configPath);
}
catch (UnauthorizedAccessException)
{
WebEventSource.Log.ApplicationInsightsConfigReadError("UnauthorizedAccessException reading config file");
}
catch (System.Security.SecurityException)
{
WebEventSource.Log.ApplicationInsightsConfigReadError("SecurityException reading config file");
}
catch (Exception ex)
{
WebEventSource.Log.ApplicationInsightsConfigReadError(ex.ToString());
}

return null;
}

/// <summary>
/// Gets the full path to the ApplicationInsights.config file.
/// </summary>
/// <returns>The full path to the config file, or null if not found.</returns>
private static string GetConfigFilePath()
{
try
{
// Config file should be in the base directory of the app domain
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName);
}
catch (System.Security.SecurityException)
{
WebEventSource.Log.ApplicationInsightsConfigReadError("SecurityException accessing AppDomain.CurrentDomain.BaseDirectory");
return null;
}
}

/// <summary>
/// Reads the connection string from the XML configuration file.
/// </summary>
/// <param name="configPath">The path to the config file.</param>
/// <returns>The connection string if found; otherwise, null.</returns>
private static string ReadConnectionStringFromConfig(string configPath)
{
XDocument xml = XDocument.Load(configPath);
XElement root = xml.Element(XmlNamespace + "ApplicationInsights");

if (root != null)
{
XElement connectionStringElement = root.Element(XmlNamespace + "ConnectionString");

if (connectionStringElement != null && !string.IsNullOrWhiteSpace(connectionStringElement.Value))
{
string connectionString = connectionStringElement.Value.Trim();
WebEventSource.Log.ApplicationInsightsConfigLoaded(configPath);
return connectionString;
}
}

WebEventSource.Log.ApplicationInsightsConfigConnectionStringNotFound(configPath);
return null;
}
}
}
48 changes: 48 additions & 0 deletions WEB/Src/Web/Web/Implementation/WebEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,54 @@ public void NoHttpApplicationWarning(string appDomainName = "Incorrect")
this.applicationNameProvider.Name);
}

[Event(
55,
Message = "ApplicationInsights.config not found at path: {0}",
Level = EventLevel.Informational)]
public void ApplicationInsightsConfigNotFound(string path, string appDomainName = "Incorrect")
{
this.WriteEvent(
55,
path ?? "NULL",
this.applicationNameProvider.Name);
}

[Event(
56,
Message = "ApplicationInsights.config loaded successfully from: {0}",
Level = EventLevel.Informational)]
public void ApplicationInsightsConfigLoaded(string path, string appDomainName = "Incorrect")
{
this.WriteEvent(
56,
path ?? "NULL",
this.applicationNameProvider.Name);
}

[Event(
57,
Message = "Failed to read ApplicationInsights.config. Error: {0}",
Level = EventLevel.Warning)]
public void ApplicationInsightsConfigReadError(string error, string appDomainName = "Incorrect")
{
this.WriteEvent(
57,
error ?? "NULL",
this.applicationNameProvider.Name);
}

[Event(
58,
Message = "ConnectionString not found in ApplicationInsights.config at path: {0}",
Level = EventLevel.Warning)]
public void ApplicationInsightsConfigConnectionStringNotFound(string path, string appDomainName = "Incorrect")
{
this.WriteEvent(
58,
path ?? "NULL",
this.applicationNameProvider.Name);
}

/// <summary>
/// Keywords for the PlatformEventSource. Those keywords should match keywords in Core.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions WEB/Src/Web/Web/applicationinsights.config.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<ConnectionString>InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://your-ingestion-endpoint.applicationinsights.azure.com/</ConnectionString>
</ApplicationInsights>
42 changes: 2 additions & 40 deletions WEB/Src/Web/Web/net462/ApplicationInsights.config.install.xdt
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
<ApplicationInsights xdt:Transform="SetAttributes" xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<TelemetryInitializers xdt:Transform="InsertIfMissing">
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.WebTestTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.SyntheticUserAgentTelemetryInitializer, Microsoft.AI.Web">
<!-- Extended list of bots:
search|spider|crawl|Bot|Monitor|BrowserMob|BingPreview|PagePeeker|WebThumb|URL2PNG|ZooShot|GomezA|Google SketchUp|Read Later|KTXN|KHTE|Keynote|Pingdom|AlwaysOn|zao|borg|oegp|silk|Xenu|zeal|NING|htdig|lycos|slurp|teoma|voila|yahoo|Sogou|CiBra|Nutch|Java|JNLP|Daumoa|Genieo|ichiro|larbin|pompos|Scrapy|snappy|speedy|vortex|favicon|indexer|Riddler|scooter|scraper|scrubby|WhatWeb|WinHTTP|voyager|archiver|Icarus6j|mogimogi|Netvibes|altavista|charlotte|findlinks|Retreiver|TLSProber|WordPress|wsr-agent|http client|Python-urllib|AppEngine-Google|semanticdiscovery|facebookexternalhit|web/snippet|Google-HTTP-Java-Client-->
<Filters>search|spider|crawl|Bot|Monitor|AlwaysOn</Filters>
</Add>
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AzureAppServiceRoleNameFromHostNameHeaderInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AuthenticatedUserIdTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AccountIdTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" />
</TelemetryInitializers>

<TelemetryModules xdt:Transform="InsertIfMissing">
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web">
<Handlers>
<!--
Add entries here to filter out additional handlers:

NOTE: handler configuration will be lost upon NuGet upgrade.
-->
<Add>Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler</Add>
<Add>System.Web.StaticFileHandler</Add>
<Add>System.Web.Handlers.AssemblyResourceLoader</Add>
<Add>System.Web.Optimization.BundleHandler</Add>
<Add>System.Web.Script.Services.ScriptHandlerFactory</Add>
<Add>System.Web.Handlers.TraceHandler</Add>
<Add>System.Web.Services.Discovery.DiscoveryRequestHandler</Add>
<Add>System.Web.HttpDebugHandler</Add>
</Handlers>
</Add>
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web" />
<Add xdt:Transform="InsertIfMissing" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AspNetDiagnosticTelemetryModule, Microsoft.AI.Web" />
</TelemetryModules>
<ApplicationIdProvider xdt:Transform="InsertIfMissing" Type="Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider, Microsoft.ApplicationInsights"/>
<!-- Learn more about Application Insights configuration with ApplicationInsights.config here: http://go.microsoft.com/fwlink/?LinkID=513840 -->
<ConnectionString xdt:Transform="InsertIfMissing"></ConnectionString>
</ApplicationInsights>
10 changes: 8 additions & 2 deletions WEB/Src/Web/Web/net462/ApplicationInsights.config.transform
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
</ApplicationInsights>
<!--
Learn more about Application Insights configuration with ApplicationInsights.config here:
http://go.microsoft.com/fwlink/?LinkID=513840

Note: If not present, please add <ConnectionString>Your Connection String</ConnectionString> to the top of this file.
-->
</ApplicationInsights>
26 changes: 2 additions & 24 deletions WEB/Src/Web/Web/net462/ApplicationInsights.config.uninstall.xdt
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

<TelemetryInitializers>
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.WebTestTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.SyntheticUserAgentTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AuthenticatedUserIdTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AccountIdTelemetryInitializer, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" />
</TelemetryInitializers>
<TelemetryInitializers xdt:Transform="Remove" xdt:Locator="Condition(count(*)=0)"/>

<TelemetryModules>
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web" />
<Add xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Web.AspNetDiagnosticTelemetryModule, Microsoft.AI.Web" />
</TelemetryModules>
<TelemetryModules xdt:Transform="Remove" xdt:Locator="Condition(count(*)=0)"/>

<ApplicationIdProvider xdt:Transform="Remove" xdt:Locator="Match(Type)" Type="Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider, Microsoft.ApplicationInsights"/>

</ApplicationInsights>
<ConnectionString xdt:Transform="Remove" />
</ApplicationInsights>
Loading