Set different InstrumentationKeys for ApplicationsInsights programmatically

332 Views Asked by At

I use Application Insights in my ASP.NET Core 3 API - and pass the InstrumentationKey from appsettings.json in Startup.cs via:

services.AddApplicationInsightsTelemetry(Configuration);

That works. But I want to use different InstrumentationKeys depending on other parameters.

How can I startup the AI with other keys (all defined in the appsettings.json or in code)?

1

There are 1 best solutions below

0
On BEST ANSWER

There are multiple ways.

If you're emitting telemetry items manually (through Track methods) then one option is to explicitly set it on an item itself. Here is an example of setting it for RequestTelemetry through TelemetryContext.InstrumentationKey property.

var requestTelemetry = new RequestTelemetry();
requestTelemetry.Context.InstrumentationKey = "<your key>";

Another option is to override this value with Telemetry Initializer.

Another option is to have a separate Telemetry Client (with a different instrumentation key). But I'm not exactly sure how to achieve this in .NET Core.