What's the difference between APPINSIGHTS_INSTRUMENTATIONKEY configured by Azure and ApplicationInsights:InstrumentationKey?
Well, the first one is when you don't host on Azure App Service or when you don't want to set an environment variable. Which one is actually used, depends on the way your configuration builder is configured.
Usually you have something like that in Startup.cs
or Programm.cs
:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddUserSecrets<Startup>()
.AddEnvironmentVariables(); // Environment Variables override all other
The order in which the .AddXxx
calls are used matter. The last registration with a matching key, will be used. Here .AddEnvironmentVariables()
is the last one. When a APPINSIGHTS_INSTRUMENTATIONKEY
variable is set, it will override all values for Appinsights:InstrumentationKey
set in user secrets, appsettings.Development.json
or appsettings.json
.
If APPINSIGHTS_INSTRUMENTATIONKEY
is not set, the configuration library will look into user secrets and use it if found. If not found it will search for appsettings.Development.json
and if it doesn't contains the value search appsettings.json
.
TL;DR: The one form appsettings.json will only be used when no environment variable is set.
Update
New Answer
As seen in the code, the Application Insight extension method to register it will override the values from either the environment variable or from the appsettings.json when it finds the matching entries.
Note: When you remove the .AddEnvironmentVariables()
it won't ever use the value set in Azure Portal, because the .AddEnvironmentVariables()
loads the environment variable into the configuration with the key APPINSIGHTS_INSTRUMENTATIONKEY
(see below).
private const string InstrumentationKeyFromConfig = "ApplicationInsights:InstrumentationKey";
private const string InstrumentationKeyForWebSites = "APPINSIGHTS_INSTRUMENTATIONKEY";
When its not found there, it tries the regular key from the appsettings.json ApplicationInsights:InstrumentationKey
.
In your example
var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
The passed value won't be used unless you both, remove the environment variable (or .AddEnvironmentVariables()
) AND remove the entry from appsettings.json
.
So for most common configuration, its enough to call
services.AddApplicationInsightsTelemetry(Configuration);
where Configuration
is the IConfigurationRoot
. This overload will load it from either Environment Variable or from appsettings.json if found.
When you want more programmatic control over it, you use the
services.AddApplicationInsightsTelemetry(options => {
// some logic here, where you can override the default behavior described above
});