public async Task <IElasticLowLevelClient> BuildAsync(ElasticClientConfiguration config, IEnumerable <string> indices) { var settings = new ConnectionConfiguration(new Uri(config.Address)); var client = new ElasticLowLevelClient(settings); var errors = new List <Exception>(); foreach (var index in indices) { var indexExists = await client.IndicesExistsAsync <string>(index).ConfigureAwait(false); if (indexExists.HttpStatusCode == null || indexExists.HttpStatusCode.Value == 404) { var postData = new { mappings = new { _default_ = new { properties = new { Timestamp = new { type = "date" } } } } }; var response = await client.IndicesCreateAsync <VoidResponse>(index, postData).ConfigureAwait(false); if (!response.Success) { errors.Add(new ElasticException(response)); } } } if (errors.Count > 0) { throw new AggregateException(errors); } return(client); }
public static void AddPetProjectElasticLogConsumer(this IServiceCollection collection, KafkaConfiguration kafkaConfig, ElasticClientConfiguration clientConfig) { collection.TryAddSingleton <ElasticClientConfiguration>(clientConfig); var topics = kafkaConfig.Topic.Split(','); var indices = topics.Select(topic => $"logs-{ topic.ToLowerInvariant() }-{ DateTime.UtcNow.ToString("dd-MM-yyyy") }").ToArray(); collection.TryAddTransient <IElasticLowLevelClientFactory, ElasticLowLevelClientFactory>(); collection.TryAddSingleton <IElasticLowLevelClient>(serviceProvider => serviceProvider.GetRequiredService <IElasticLowLevelClientFactory>().BuildAsync(serviceProvider.GetRequiredService <ElasticClientConfiguration>(), indices).Result); collection.AddLogging(cfg => cfg.AddConsole()); collection.TryAddSingleton <IPetProjectLogConsumerLogger>(sp => { var providers = sp.GetServices <ILoggerProvider>(); return(new PetProjectLogConsumerLogger(providers.First(provider => provider is ConsoleLoggerProvider))); }); for (var i = 0; i < topics.Length; i++) { var topic = topics[i]; var index = indices[i]; // this will make the provider own the consumer instance, i.e., the consumer will be disposed automatically by the DI container // if we added the singleton like this: collection.AddSingleton(new PetProjectLogConsumer()); then it wouldn't be disposed // automatically because the DI didn't actually create the instance so it doesn't own it collection.AddSingleton <PetProjectLogConsumer>(sp => new PetProjectLogConsumer( kafkaConfig.Brokers, kafkaConfig.ConsumerGroupId, topic, new ElasticLogEventV1Store(sp.GetRequiredService <IElasticLowLevelClient>(), index), sp.GetRequiredService <IPetProjectLogConsumerLogger>())); } }