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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With