microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

Logging in ASP.NET Core using Application Insights #319

Closed bytesfromali closed 5 years ago

bytesfromali commented 5 years ago

Is there any way to switch instrumentation key on the run, based on some business logic to target log destination to different Application Insights instances.

As we configure Application Insights to be used as Logger in startup project, so I can't find any out of the box approach or hack to a workaround for this requirement.

The above switching can be easily done if I am using Application insight as a telemetry client because in this case, I can provide different instrumentation keys on the run to the client which is not the same for ILogger getting injected using built-in DI of ASP.NET Core.

cijothomas commented 5 years ago

Have you tried writing a telemetryinitializer to override the instrumentation key?

internal class MyTelemetryInstrumentationKeyOverrider : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            // conditionally do this.
            telemetry.Context.InstrumentationKey = "newkey";
        }
    }