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);
            }
            public static void DoesNotThrowWithoutInstrumentationKey()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                //Empty configuration that doesn't have instrumentation key
                IConfiguration config = new ConfigurationBuilder().Build();

                services.AddApplicationInsightsTelemetry(config);
            }
            public static void RegistersExpectedServices(Type serviceType, Type implementationType, ServiceLifetime lifecycle)
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                ServiceDescriptor service = services.Single(s => s.ServiceType == serviceType && s.ImplementationType == implementationType);

                Assert.Equal(lifecycle, service.Lifetime);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatCreatesDefaultInstance()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Contains(telemetryConfiguration.TelemetryInitializers, t => t is TimestampPropertyInitializer);
            }
            public static void DoesNotOverrideDefaultTelemetryChannelIfTelemetryChannelServiceIsNotRegistered()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.NotNull(telemetryConfiguration.TelemetryChannel);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromConfiguration()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                var config   = new ConfigurationBuilder().AddJsonFile("content\\config-developer-mode.json").Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromConfiguration()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                var config   = new ConfigurationBuilder().AddJsonFile("content\\config-instrumentation-key.json").Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Equal("11111111-2222-3333-4444-555555555555", telemetryConfiguration.InstrumentationKey);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromSettings()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                var config   = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/").Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Equal("http://localhost:1234/v2/track/", telemetryConfiguration.TelemetryChannel.EndpointAddress);
            }
            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 RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithContextInitializersFromContainer()
            {
                var contextInitializer = new FakeContextInitializer();
                var services           = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddInstance <IContextInitializer>(contextInitializer);

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Contains(contextInitializer, telemetryConfiguration.ContextInitializers);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", "http://localhost:1234/v2/track/");
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal("http://localhost:1234/v2/track/", telemetryConfiguration.TelemetryChannel.EndpointAddress);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", null);
                }
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", "true");
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", null);
                }
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", "11111111-2222-3333-4444-555555555555");
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal("11111111-2222-3333-4444-555555555555", telemetryConfiguration.InstrumentationKey);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null);
                }
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithModulesFromContainer()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var modules = serviceProvider.GetServices <ITelemetryModule>();

                Assert.NotNull(modules);
                Assert.Equal(2, modules.Count());

                var dependencyModule = services.FirstOrDefault <ServiceDescriptor>(t => t.ImplementationType == typeof(DependencyTrackingTelemetryModule));

                Assert.NotNull(dependencyModule);

                var perfCounterModule = services.FirstOrDefault <ServiceDescriptor>(t => t.ImplementationType == typeof(PerformanceCollectorModule));

                Assert.NotNull(perfCounterModule);
            }