Skip to content
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

DefaultApplicationInsightsServiceConfigureOptions overwrites user configuration #2900

Open
ascott18 opened this issue Sep 17, 2024 · 0 comments
Labels

Comments

@ascott18
Copy link

  • List of NuGet packages and version that you are using: Microsoft.ApplicationInsights.AspNetCore 2.22.0
  • Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the *.csproj file): net8
  • Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.): Windows

Describe the bug

My app has some slightly custom configuration files, as follows:

Program.cs

builder.Configuration
	.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
	.AddJsonFile("appsettings.localhost.json", optional: true, reloadOnChange: true)
	.AddEnvironmentVariables();

builder.Services.AddApplicationInsightsTelemetry();

appsettings.json (git committed, baseline configuration)

{
  "ApplicationInsights": {
    "ConnectionString": "<Connection string Alpha>"
  },
  // Other stuff
}

appsettings.localhost.json (gitignored, local machine configuration)

{
  "ApplicationInsights": {
    "ConnectionString": "<Connection string Delta>"
  },
  // Other stuff
}

Unfortunately, app insights is ignoring what's in appsettings.localhost.json because after acquiring my own custom configuration, it overlays its own configuration on top of that:

if (this.userConfiguration != null)
{
configBuilder.AddConfiguration(this.userConfiguration);
}
configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();

In my configuration setup, connection string Delta is the last one defined and should therefore "win", but appinsights is re-applying appsettings.json on top of that and therefore Alpha becomes the connection string that gets used.

App insights should not be overriding the user-provided configuration with additional default configuration files. These defaults should be applied to the internal configuration builder first, and then the userConfiguration should be overlayed on top of that so that user configuration is respected.

To Reproduce

Setup a plain aspnetcore project with the above 3 config files.

@ascott18 ascott18 added the bug label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant