private static void ConfigureServices(IServiceCollection serviceCollection, IConfiguration configuration) { //Add logging serviceCollection.AddLogging(loggingBuilder => { loggingBuilder.AddConfiguration(configuration.GetSection("Logging")); string instrumentationKey = configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]; if (!string.IsNullOrEmpty(instrumentationKey)) { loggingBuilder.AddApplicationInsights(instrumentationKey); } #if DEBUG loggingBuilder.AddSerilog(new LoggerConfiguration() .WriteTo.File("Logs/UKHO.ExchangeSetService.CleanUpLogs-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}") .MinimumLevel.Information() .MinimumLevel.Override("UKHO", LogEventLevel.Debug) .CreateLogger(), dispose: true); #endif loggingBuilder.AddConsole(); loggingBuilder.AddDebug(); EventHubLoggingConfiguration eventhubConfig = configuration.GetSection("EventHubLoggingConfiguration").Get <EventHubLoggingConfiguration>(); if (!string.IsNullOrWhiteSpace(eventhubConfig.ConnectionString)) { loggingBuilder.AddEventHub(config => { config.Environment = eventhubConfig.Environment; config.DefaultMinimumLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), eventhubConfig.MinimumLoggingLevel, true); config.MinimumLogLevels["UKHO"] = (LogLevel)Enum.Parse(typeof(LogLevel), eventhubConfig.UkhoMinimumLoggingLevel, true); config.EventHubConnectionString = eventhubConfig.ConnectionString; config.EventHubEntityPath = eventhubConfig.EntityPath; config.System = eventhubConfig.System; config.Service = eventhubConfig.Service; config.NodeName = eventhubConfig.NodeName; config.AdditionalValuesProvider = additionalValues => { additionalValues["_AssemblyVersion"] = AssemblyVersion; }; }); } }); serviceCollection.Configure <TelemetryConfiguration>( (config) => { config.TelemetryChannel = aiChannel; } ); serviceCollection.Configure <EssFulfilmentStorageConfiguration>(configuration.GetSection("EssFulfilmentStorageConfiguration")); serviceCollection.Configure <CleanUpConfiguration>(configuration.GetSection("CleanUpConfiguration")); serviceCollection.AddSingleton <IConfiguration>(configuration); serviceCollection.AddTransient <ExchangeSetCleanUpJob>(); serviceCollection.AddScoped <IAzureBlobStorageClient, AzureBlobStorageClient>(); serviceCollection.AddScoped <IExchangeSetCleanUpService, ExchangeSetCleanUpService>(); serviceCollection.AddScoped <ISalesCatalogueStorageService, SalesCatalogueStorageService>(); serviceCollection.AddScoped <IAzureFileSystemHelper, AzureFileSystemHelper>(); }
private static HostBuilder BuildHostConfiguration() { HostBuilder hostBuilder = new HostBuilder(); hostBuilder.ConfigureAppConfiguration((hostContext, builder) => { builder.AddJsonFile("appsettings.json"); //Add environment specific configuration files. var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); if (!string.IsNullOrWhiteSpace(environmentName)) { builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true); } var tempConfig = builder.Build(); string kvServiceUri = tempConfig["KeyVaultSettings:ServiceUri"]; if (!string.IsNullOrWhiteSpace(kvServiceUri)) { var secretClient = new SecretClient(new Uri(kvServiceUri), new DefaultAzureCredential( new DefaultAzureCredentialOptions { ManagedIdentityClientId = tempConfig["ESSManagedIdentity:ClientId"] })); builder.AddAzureKeyVault(secretClient, new KeyVaultSecretManager()); } #if DEBUG //Add development overrides configuration builder.AddJsonFile("appsettings.local.overrides.json", true, true); #endif //Add environment variables builder.AddEnvironmentVariables(); Program.ConfigurationBuilder = builder.Build(); }) .ConfigureLogging((hostContext, builder) => { builder.AddConfiguration(ConfigurationBuilder.GetSection("Logging")); #if DEBUG builder.AddSerilog(new LoggerConfiguration() .WriteTo.File("Logs/UKHO.ExchangeSetService.FulfilmentServiceLogs-.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}") .MinimumLevel.Information() .MinimumLevel.Override("UKHO", LogEventLevel.Debug) .CreateLogger(), dispose: true); #endif builder.AddConsole(); //Add Application Insights if needed (if key exists in settings) string instrumentationKey = ConfigurationBuilder["APPINSIGHTS_INSTRUMENTATIONKEY"]; if (!string.IsNullOrEmpty(instrumentationKey)) { builder.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey); } EventHubLoggingConfiguration eventhubConfig = ConfigurationBuilder.GetSection("EventHubLoggingConfiguration").Get <EventHubLoggingConfiguration>(); if (!string.IsNullOrWhiteSpace(eventhubConfig.ConnectionString)) { builder.AddEventHub(config => { config.Environment = eventhubConfig.Environment; config.DefaultMinimumLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), eventhubConfig.MinimumLoggingLevel, true); config.MinimumLogLevels["UKHO"] = (LogLevel)Enum.Parse(typeof(LogLevel), eventhubConfig.UkhoMinimumLoggingLevel, true); config.EventHubConnectionString = eventhubConfig.ConnectionString; config.EventHubEntityPath = eventhubConfig.EntityPath; config.System = eventhubConfig.System; config.Service = eventhubConfig.Service; config.NodeName = eventhubConfig.NodeName; config.AdditionalValuesProvider = additionalValues => { additionalValues["_AssemblyVersion"] = AssemblyVersion; }; }); } }) .ConfigureServices((hostContext, services) => { var buildServiceProvider = services.BuildServiceProvider(); services.Configure <EssFulfilmentStorageConfiguration>(ConfigurationBuilder.GetSection("EssFulfilmentStorageConfiguration")); services.Configure <CacheConfiguration>(ConfigurationBuilder.GetSection("CacheConfiguration")); services.Configure <QueuesOptions>(ConfigurationBuilder.GetSection("QueuesOptions")); services.Configure <SalesCatalogueConfiguration>(ConfigurationBuilder.GetSection("SalesCatalogue")); services.AddScoped <IEssFulfilmentStorageConfiguration, EssFulfilmentStorageConfiguration>(); services.AddScoped <ISalesCatalogueStorageService, SalesCatalogueStorageService>(); services.AddScoped <IFulfilmentDataService, FulfilmentDataService>(); services.AddScoped <IMonitorHelper, MonitorHelper>(); services.AddScoped <IAzureBlobStorageService, AzureBlobStorageService>(); services.AddScoped <IAzureBlobStorageClient, AzureBlobStorageClient>(); services.AddScoped <IAzureMessageQueueHelper, AzureMessageQueueHelper>(); services.AddScoped <IAzureTableStorageClient, AzureTableStorageClient>(); services.AddScoped <IFileShareServiceCache, FileShareServiceCache>(); var retryCount = Convert.ToInt32(ConfigurationBuilder["RetryConfiguration:RetryCount"]); var sleepDuration = Convert.ToDouble(ConfigurationBuilder["RetryConfiguration:SleepDuration"]); services.AddHttpClient <IFileShareServiceClient, FileShareServiceClient>(client => { client.BaseAddress = new Uri(ConfigurationBuilder["FileShareService:BaseUrl"]); var productHeaderValue = new ProductInfoHeaderValue(ExchangeSetServiceUserAgent, AssemblyVersion); client.DefaultRequestHeaders.UserAgent.Add(productHeaderValue); client.Timeout = TimeSpan.FromMinutes(Convert.ToDouble(ConfigurationBuilder["FileShareService:TimeOutInMins"])); }) .AddPolicyHandler((services, request) => CommonHelper.GetRetryPolicy(services.GetService <ILogger <IFileShareServiceClient> >(), "File Share", EventIds.RetryHttpClientFSSRequest, retryCount, sleepDuration)); services.AddHttpClient <ISalesCatalogueClient, SalesCatalogueClient>(client => { client.BaseAddress = new Uri(ConfigurationBuilder["SalesCatalogue:BaseUrl"]); var productHeaderValue = new ProductInfoHeaderValue(ExchangeSetServiceUserAgent, AssemblyVersion); client.DefaultRequestHeaders.UserAgent.Add(productHeaderValue); }) .AddPolicyHandler((services, request) => CommonHelper.GetRetryPolicy(services.GetService <ILogger <ISalesCatalogueClient> >(), "Sales Catalogue", EventIds.RetryHttpClientSCSRequest, retryCount, sleepDuration)); services.AddHttpClient <ICallBackClient, CallBackClient>(); services.AddSingleton <IAuthFssTokenProvider, AuthFssTokenProvider>(); services.AddSingleton <IAuthScsTokenProvider, AuthScsTokenProvider>(); services.AddScoped <IFileShareService, FileShareService>(); services.AddScoped <IFulfilmentFileShareService, FulfilmentFileShareService>(); services.AddScoped <IFulfilmentAncillaryFiles, FulfilmentAncillaryFiles>(); services.AddScoped <IFileSystemHelper, FileSystemHelper>(); services.AddScoped <ISalesCatalogueService, SalesCatalogueService>(); services.AddScoped <IFulfilmentSalesCatalogueService, FulfilmentSalesCatalogueService>(); services.AddSingleton <ISmallExchangeSetInstance, SmallExchangeSetInstance>(); services.AddSingleton <IMediumExchangeSetInstance, MediumExchangeSetInstance>(); services.AddSingleton <ILargeExchangeSetInstance, LargeExchangeSetInstance>(); services.AddScoped <IFulfilmentCallBackService, FulfilmentCallBackService>(); services.Configure <FileShareServiceConfiguration>(ConfigurationBuilder.GetSection("FileShareService")); services.Configure <EssManagedIdentityConfiguration>(ConfigurationBuilder.GetSection("ESSManagedIdentity")); services.Configure <EssCallBackConfiguration>(ConfigurationBuilder.GetSection("ESSCallBackConfiguration")); services.AddDistributedMemoryCache(); // Add App Insights Telemetry Filter var telemetryConfiguration = buildServiceProvider.GetRequiredService <TelemetryConfiguration>(); var telemetryProcessorChainBuilder = telemetryConfiguration.TelemetryProcessorChainBuilder; telemetryProcessorChainBuilder.Use(next => new AzureDependencyFilterTelemetryProcessor(next)); telemetryProcessorChainBuilder.Build(); }) .ConfigureWebJobs(b => { b.AddAzureStorageCoreServices(); b.AddAzureStorage(); }); return(hostBuilder); }