Konrad
Konrad

Reputation: 4772

Set different InstrumentationKeys for ApplicationsInsights programmatically

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)?

Upvotes: 0

Views: 397

Answers (1)

ZakiMa
ZakiMa

Reputation: 6241

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.

Upvotes: 2

Related Questions