public static IServiceCollection AddAppInsights(this IServiceCollection services, IConfiguration configuration, IKeyVaultClient kvClient)
        {
            var appInsightsConfig = TelemetryConfiguration.Active;
            var vaultSettings     = new VaultSettings();

            configuration.Bind("Vault", vaultSettings);

            var instrumentationKey = kvClient.GetSecretAsync(
                $"https://{vaultSettings.Name}.vault.azure.net",
                configuration["AppInsights:InstrumentationKeySecret"])
                                     .GetAwaiter().GetResult();

            appInsightsConfig.InstrumentationKey = instrumentationKey.Value;
            appInsightsConfig.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
            appInsightsConfig.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
            var serviceContext = new ServiceContext();

            configuration.Bind("AppInsights:Context", serviceContext);
            appInsightsConfig.TelemetryInitializers.Add(new ContextTelemetryInitializer(serviceContext));

            var module = new DependencyTrackingTelemetryModule();

            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs");
            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.KeyVault");
            module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.DocumentDB");
            module.Initialize(appInsightsConfig);

            var telemetryClient = new TelemetryClient();

            telemetryClient.TrackTrace("Program started...");
            services.AddSingleton(telemetryClient);

            return(services);
        }
        public static IServiceCollection AddKeyVault(this IServiceCollection services, IConfiguration configuration)
        {
            var vaultSettings = new VaultSettings();
            configuration.Bind("Vault", vaultSettings);
            KeyVaultClient.AuthenticationCallback callback = async (authority, resource, scope) =>
            {
                var authContext = new AuthenticationContext(authority);
                var certificate = new X509Certificate2(vaultSettings.ClientCertFile);
                var clientCred = new ClientAssertionCertificate(vaultSettings.ClientId, certificate);
                var result = await authContext.AcquireTokenAsync(resource, clientCred);

                if (result == null)
                    throw new InvalidOperationException("Failed to obtain the JWT token");

                return result.AccessToken;
            };
            var kvClient = new KeyVaultClient(callback);
            services.AddSingleton<IKeyVaultClient>(kvClient);

            return services;
        }