Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ITelemetry.InstrumentationKey is empty

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.

like image 705
Anish Avatar asked Oct 29 '25 08:10

Anish


1 Answers

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.

like image 71
Ivan Yang Avatar answered Oct 31 '25 22:10

Ivan Yang