ASP.NET Core configuration preferentially treats appsettings file instead of IConfiguration
See original GitHub issueASP.NET Core SDK only
Actual:
AddApplicationInsightsTelemetry
is “favoring” configuration from appsettings.json
.
Expected:
AddApplicationInsightsTelemetry
should simply respect IConfiguration
, which is fully controlled by application owner.
To Reproduce
See https://github.com/MicrosoftDocs/azure-docs/issues/90862
Rootcause:
https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs#L36-L43
^ This first loads user’s IConfiguration
, then loads appsettings.json
. This causes appsettings.json to override.
WorkerService does not have this issue. So, I think this added to maintain backward compatibility when the feature was added to AddApplicationInsightsTelemetry() to automatically read IConfiguration
.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:5 (2 by maintainers)
Perhaps we need an overload of
AddApplicationInsightsTelemetry
that takes anIConfiguration
and a delegate (Action<ApplicationInsightsServiceOptions>
)Alternatively, does this work:
where
configuration
is your configuration root object. If your code doesn’t already have it in hand, it may be available on the builder context object.Being new to AppInsights myself, I had to digest all of this and realized that I should be using the method signature which takes in my version of IConfiguration. The documentation does call it out but should likely better reinforce this concept and explain the behavior. I would expect IConfiguration to be used by default and for it to be the sole source of truth and never overridden by re-reading from appsettings.json as I have overloads i.e. the typical per-environment file approach.
What I found after switching to that method signature was that I lost the ability to tweak the ‘options’ in conjunction with loading from the referenced instance of IConfiguration - which I had been leveraging that to explicitly set the Developer Mode based on my own specific logic.
It seems like now I have to find the equivalent way to explicitly set it in my appsettings.json file - or overload files - otherwise it looks like I’d have to set if by code where I resolve & use the TelemetryConfiguration/Channel (I think). I’d much rather do it once in the same place I’m registering things.