ApplicationInsightsExtensions

We found 10 examples in language CSharp for this search. You will see 15 fragments of code.
        [Fact(DisplayName = "The required services are properly injected")]
        public void ServicesRegistered()
        {
            IServiceCollection services = new ServiceCollection();
            services.AddSingleton<IConfiguration>(p => new ConfigurationBuilder().Build());

            ApplicationInsightsExtensions.ConfigureKubernetesTelemetryInitializer(services, () => true, new KubernetesTestServiceCollectionBuilder(), null);
            Assert.NotNull(services.FirstOrDefault(sd => sd.ImplementationType == typeof(KubernetesTelemetryInitializer)));

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITelemetryInitializer targetTelemetryInitializer = serviceProvider.GetServices<ITelemetryInitializer>().First(ti => ti is KubernetesTelemetryInitializer);
            Assert.NotNull(targetTelemetryInitializer);

            // K8s services	
            serviceProvider.GetRequiredService<IKubeHttpClientSettingsProvider>();
            serviceProvider.GetRequiredService<KubeHttpClientFactory>();
            serviceProvider.GetRequiredService<K8sQueryClientFactory>();
            serviceProvider.GetRequiredService<IK8sEnvironmentFactory>();
        }

        [Fact(DisplayName = "Default timeout for waiting container to spin us is 2 minutes")]
        public void EnableAppInsightsForKubernetesWithDefaultTimeOut()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddSingleton<IConfiguration>(p => new ConfigurationBuilder().Build());

            serviceCollection.AddApplicationInsightsKubernetesEnricher(
                applyOptions: null,
                kubernetesServiceCollectionBuilder: new KubernetesTestServiceCollectionBuilder(),
                detectKubernetes: () => true);
            ITelemetryInitializer targetTelemetryInitializer = serviceCollection.BuildServiceProvider().GetServices<ITelemetryInitializer>().First(i => i is KubernetesTelemetryInitializer);

            if (targetTelemetryInitializer is KubernetesTelemetryInitializer target)
            {
                Assert.StrictEqual(TimeSpan.FromMinutes(2), target._options.InitializationTimeout);
            }
            else
            {
                Assert.True(false, "Not the target telemetry initializer.");
            }
        }

        [Fact(DisplayName = "Set timeout through options works for telemetry initializer.")]
        public void EnableAppInsightsForKubernetesWithTimeOutSetThroughOptions()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddSingleton<IConfiguration>(p => new ConfigurationBuilder().Build());
            serviceCollection.AddApplicationInsightsKubernetesEnricher(
                    applyOptions: option =>
                    {
                        option.InitializationTimeout = TimeSpan.FromSeconds(5);
                    },
                    kubernetesServiceCollectionBuilder: new KubernetesTestServiceCollectionBuilder(),
                    detectKubernetes: () => true);
            ITelemetryInitializer targetTelemetryInitializer = serviceCollection.BuildServiceProvider().GetServices<ITelemetryInitializer>().First(i => i is KubernetesTelemetryInitializer);

            if (targetTelemetryInitializer is KubernetesTelemetryInitializer target)
            {
                Assert.StrictEqual(TimeSpan.FromSeconds(5), target._options.InitializationTimeout);
            }
            else
            {
                Assert.True(false, "Not the target telemetry initializer.");
            }
        }

        [Fact(DisplayName = "Set timeout through configuration works for telemetry initializer.")]
        public void EnableAppInsightsForKubernetesWithTimeOutSetThroughConfiguration()
        {
            IServiceCollection services = new ServiceCollection();
            IConfiguration config = new ConfigurationBuilder().AddInMemoryCollection(
                new Dictionary<string, string>(){
                    {"a" , "b"},
                    {"AppInsightsForKubernetes:InitializationTimeout", "3.1:12:15.34"}
            }).Build();
            services.AddSingleton(config);

            ApplicationInsightsExtensions.ConfigureKubernetesTelemetryInitializer(services, () => true, new KubernetesTestServiceCollectionBuilder(), null);
            Assert.NotNull(services.FirstOrDefault(sd => sd.ImplementationType == typeof(KubernetesTelemetryInitializer)));

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITelemetryInitializer targetTelemetryInitializer = serviceProvider.GetServices<ITelemetryInitializer>().FirstOrDefault(ti => ti is KubernetesTelemetryInitializer);

            if (targetTelemetryInitializer is KubernetesTelemetryInitializer target)
            {
                Assert.StrictEqual(new TimeSpan(days: 3, hours: 1, minutes: 12, seconds: 15, milliseconds: 340), target._options.InitializationTimeout);
            }
            else
            {
                Assert.True(false, "Not the target telemetry initializer.");
            }
        }

        [Fact(DisplayName = "Options by code takes precedence of configuration.")]
        public void EnableAppInsightsForKubernetesWithTimeOutSetThroughOptionsOverwritingConfiugure()
        {
            IServiceCollection services = new ServiceCollection();
            IConfiguration config = new ConfigurationBuilder().AddInMemoryCollection(
                new Dictionary<string, string>(){
                    {"a" , "b"},
                    {"AppInsightsForKubernetes:InitializationTimeout", "3.1:12:15.34"}
            }).Build();
            services.AddSingleton(config);

            ApplicationInsightsExtensions.ConfigureKubernetesTelemetryInitializer(services, () => true, new KubernetesTestServiceCollectionBuilder(),
                option =>
                {
                    option.InitializationTimeout = TimeSpan.FromSeconds(30);
                });
            Assert.NotNull(services.FirstOrDefault(sd => sd.ImplementationType == typeof(KubernetesTelemetryInitializer)));

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITelemetryInitializer targetTelemetryInitializer = serviceProvider.GetServices<ITelemetryInitializer>().FirstOrDefault(ti => ti is KubernetesTelemetryInitializer);

            if (targetTelemetryInitializer is KubernetesTelemetryInitializer target)
            {
                Assert.StrictEqual(TimeSpan.FromSeconds(30), target._options.InitializationTimeout);
            }
            else
            {
                Assert.True(false, "Not the target telemetry initializer.");
            }
        }

        [Fact(DisplayName = "Support adding KubernetesTelemetryInitializer to given TelemetryConfiguration")]
        [Obsolete("API to be removed later.", error: false)]
        public void AddTheInitializerToGivenConfiguration()
        {
            TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration();
            telemetryConfiguration.AddApplicationInsightsKubernetesEnricher(null, new KubernetesTestServiceCollectionBuilder(), () => true);
            Assert.NotNull(telemetryConfiguration.TelemetryInitializers);
            Assert.Single(telemetryConfiguration.TelemetryInitializers);
            Assert.True(telemetryConfiguration.TelemetryInitializers.First() is KubernetesTelemetryInitializer);
        }

        public static ILoggerFactory AddApplicationInsightsLoggingService(
            this ILoggerFactory factory,
            IApplicationBuilder app,
            LogLevel minLevel)
        {
            var telemetryClient = app.ApplicationServices.GetService<TelemetryClient>();
            factory.AddProvider(new ApplicationInsightsServiceProvider(telemetryClient, minLevel));
            return factory;
        }


        public static IServiceCollection AddApplicationInsights(this IServiceCollection services, IConfiguration configuration)
        {
            var configurationSection = configuration.GetSection(ApplicationInsights);
            var cloudRoleName = configurationSection[CloudRoleName];
            services.AddSingleton<ITelemetryInitializer>(provider => new CloudRoleNameTelemetryInitializer(cloudRoleName));
            services.AddTransient<ApplicationInsightsLoggingMiddleware>();

            return services;
        }

        public static IServiceCollection AddCreyApplicationInsights(this IServiceCollection services, IConfiguration config)
        {
            var aiOptions = new ApplicationInsightsServiceOptions();

            aiOptions.InstrumentationKey = config.GetApplicationInsightKey();

            services.AddHttpContextAccessor();
            services.AddApplicationInsightsTelemetry(aiOptions);
            return services;
        }

        public static void AddCustomizedApplicationInsightsTelemetry(this IServiceCollection services,
            IConfiguration configuration, string roleName, string roleNameInstance)
        {
            var instrumentationKey = configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
            if (string.IsNullOrEmpty(instrumentationKey))
            {
                throw new ArgumentNullException(nameof(instrumentationKey));
            }

            services.AddApplicationInsightsTelemetry(o =>
            {
                o.InstrumentationKey = instrumentationKey;
            });

            services.AddSingleton<ITelemetryInitializer>(new RoleNameInitializer(roleName, roleNameInstance));
        }


        public static IServiceCollection AddApplicationInsights(this IServiceCollection services, IConfiguration configuration)
        {
            var configurationSection = configuration.GetSection(ApplicationInsights);
            var cloudRoleName = configurationSection[CloudRoleName];
            services.AddSingleton<ITelemetryInitializer>(provider => new CloudRoleNameTelemetryInitializer(cloudRoleName));
            services.AddTransient<ApplicationInsightsLoggingMiddleware>();

            return services;
        }

        public static void AddCustomizedApplicationInsightsTelemetry(this IServiceCollection services,
            IConfiguration configuration, string roleName, string roleNameInstance)
        {
            var instrumentationKey = configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
            if (string.IsNullOrEmpty(instrumentationKey))
            {
                throw new ArgumentNullException(nameof(instrumentationKey));
            }

            services.AddApplicationInsightsTelemetry(o =>
            {
                o.InstrumentationKey = instrumentationKey;
            });

            services.AddSingleton<ITelemetryInitializer>(new RoleNameInitializer(roleName, roleNameInstance));
        }

        public static void AddLiveMetrisApplicationInsights(this TelemetryConfiguration appInsightsConfig)
        {
            var builder = appInsightsConfig.TelemetryProcessorChainBuilder;
            QuickPulseTelemetryProcessor quickPulseProcessor = null;
            builder.Use((next) =>
            {
                quickPulseProcessor = new QuickPulseTelemetryProcessor(next);
                return quickPulseProcessor;
            });
            builder.Build();

            var quickPulse = new QuickPulseTelemetryModule();
            quickPulse.Initialize(appInsightsConfig);
            quickPulse.RegisterTelemetryProcessor(quickPulseProcessor);
        }

        public static void AddCustomizedApplicationInsightsTelemetry(this IServiceCollection services,
            IConfiguration configuration, string roleName, string roleNameInstance)
        {
            var instrumentationKey = configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
            if (string.IsNullOrEmpty(instrumentationKey))
            {
                throw new ArgumentNullException(nameof(instrumentationKey));
            }

            services.AddApplicationInsightsTelemetry(o =>
            {
                o.InstrumentationKey = instrumentationKey;
            });

            services.AddSingleton<ITelemetryInitializer>(new RoleNameInitializer(roleName, roleNameInstance));
        }

        public static IHtmlString ApplicationInsights(this SitecoreHelper helper)
        {
            return new HtmlString(Scripts.KeyScript + Scripts.ClientScript);
        }

Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions : Object

Methods :

public static IServiceCollection AddApplicationInsightsKubernetesEnricher(IServiceCollection services = )
public static IServiceCollection AddApplicationInsightsKubernetesEnricher(IServiceCollection services = , Action<AppInsightsForKubernetesOptions> applyOptions = )
public static IServiceCollection AddApplicationInsightsKubernetesEnricher(IServiceCollection services = , Action<AppInsightsForKubernetesOptions> applyOptions = , IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = , Func<Boolean> detectKubernetes = )
public static Void AddApplicationInsightsKubernetesEnricher(TelemetryConfiguration telemetryConfiguration = , Action<AppInsightsForKubernetesOptions> applyOptions = null, IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null, Func<Boolean> detectKubernetes = null)
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()