question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ASP.NET Core configuration preferentially treats appsettings file instead of IConfiguration

See original GitHub issue

ASP.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.

https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs#L31https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/NETCORE/src/Microsoft.ApplicationInsights.WorkerService/DefaultApplicationInsightsServiceConfigureOptions.cs#L31

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:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pharringcommented, Apr 26, 2022

… I lost the ability to tweak the ‘options’ in conjunction with loading from the referenced instance of IConfiguration

Perhaps we need an overload of AddApplicationInsightsTelemetry that takes an IConfiguration and a delegate (Action<ApplicationInsightsServiceOptions>)

Alternatively, does this work:

services.AddApplicationInsightsTelemetry(options => 
{
    configuration.Bind("ApplicationInsights", options);
    // TODO: Modify options.DeveloperMode as required
});

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.

1reaction
bhehecommented, Apr 26, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Read more >
Configuration differences between ASP.NET MVC and ...
In ASP.NET apps, configuration uses the built-in .NET configuration files, web.config in the app folder and machine.config on the server.
Read more >
NET 6 (stable) IConfiguration setup in Program.cs
IConfiguration is basically the file appsettings.json in ASP.NET Core. In old code it looks like this: public class Startup { internal ...
Read more >
A Better Way to Inject AppSettings in Asp.NET Core
To do this, all you need to do is set it up as a service on your Startup.cs file. Startup.cs has a Configuration...
Read more >
Options Pattern in ASP.NET Core - Bind & Validate ...
IConfiguration is a simple and lightweight approach for loading configurations in ASP.NET Core applications from the appsettings file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found