-
Notifications
You must be signed in to change notification settings - Fork 295
[Shim] .NET Framework - Support ConnStr from ApplicationInsights.config #3018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
WEB/Src/Web/Web/Implementation/ApplicationInsightsConfigurationReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
42
WEB/Src/Web/Web/net462/ApplicationInsights.config.install.xdt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
10
WEB/Src/Web/Web/net462/ApplicationInsights.config.transform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
26
WEB/Src/Web/Web/net462/ApplicationInsights.config.uninstall.xdt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.