Reputation: 3172
I am trying to set the InstrumentationKey of AppInsights TTelemetry from appsettings.json. My code looks like this
Program.cs
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
});
Startup.cs, ConfigureServices()
services.AddApplicationInsightsTelemetry(Configuration);
appsettings.json
"ApplicationInsights": {
"InstrumentationKey": "[my key]"
},
However, when I run _serviceProvider.GetService(), or recieved the TelemetryClient via dependecy injection in the controller, the telemetry.Context.InstrumentationKey is empty.
Upvotes: 4
Views: 3049
Reputation: 29985
This is by design.
TelemetryClient.InstrumentationKey or TelemetryClient.Context.InstrumentationKey
should be empty unless you explicitly set it there as an override of what is in configuration.
As mentioned above, explicitly set it like: TelemetryClient client = new TelemetryClient() { InstrumentationKey= "your_ikey" };
, then you can use .Context.InstrumentationKey to get the key.
And you can find the details on this issue.
Hope it helps.
Upvotes: 2