public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryChannelFromContainer() { var telemetryChannel = new FakeTelemetryChannel(); var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor(); services.AddInstance<ITelemetryChannel>(telemetryChannel); services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build()); IServiceProvider serviceProvider = services.BuildServiceProvider(); var telemetryConfiguration = serviceProvider.GetRequiredService<TelemetryConfiguration>(); Assert.Same(telemetryChannel, telemetryConfiguration.TelemetryChannel); }
public static void RegistersTelemetryClientToGetTelemetryConfigurationFromContainerAndNotGlobalInstance() { ITelemetry sentTelemetry = null; var telemetryChannel = new FakeTelemetryChannel { OnSend = telemetry => sentTelemetry = telemetry }; var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor(); services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build()); IServiceProvider serviceProvider = services.BuildServiceProvider(); var configuration = serviceProvider.GetRequiredService<TelemetryConfiguration>(); configuration.InstrumentationKey = Guid.NewGuid().ToString(); configuration.TelemetryChannel = telemetryChannel; var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>(); telemetryClient.TrackEvent("myevent"); // We want to check that configuration from contaier was used but configuration is a private field so we check // instrumentation key instead Assert.Equal(configuration.InstrumentationKey, sentTelemetry.Context.InstrumentationKey); }