private void ExportToElasticSearch(EventLogExportSettingsForElasticSearch eventLogSettings) { ConnectionSettings elasticSettings = new ConnectionSettings(eventLogSettings.NodeAddress) .DefaultIndex(eventLogSettings.IndexName) .MaximumRetries(eventLogSettings.MaximumRetries) .MaxRetryTimeout(TimeSpan.FromSeconds(eventLogSettings.MaxRetryTimeout)); EventLogOnElasticSearch target = new EventLogOnElasticSearch(elasticSettings, eventLogSettings.Portion); target.SetInformationSystem(new InformationSystemsBase() { Name = eventLogSettings.InforamtionSystemName, Description = eventLogSettings.InforamtionSystemDescription }); target.SetIndexName(eventLogSettings.IndexName); target.SetIndexSeparationPeriod(eventLogSettings.IndexSeparation); ElasticClient client = new ElasticClient(elasticSettings); var allIndices = client.Indices.Get(new GetIndexRequest(Indices.All)); foreach (var indexInfo in allIndices.Indices) { if (indexInfo.Key.Name.StartsWith(eventLogSettings.IndexName)) { client.Indices.Delete(indexInfo.Key.Name); } } ExportHelperForElasticSearch.ExportToTargetStorage(eventLogSettings, target); Thread.Sleep(30000); long rowsInES = 0; string indexNameLGF_WithData = $"{eventLogSettings.IndexName}-logdata"; var allIndicesCheckData = client.Indices.Get(new GetIndexRequest(Indices.All)); foreach (var indexInfo in allIndicesCheckData.Indices) { if (indexInfo.Key.Name.StartsWith(indexNameLGF_WithData)) { var countResponse = client.Count <LogDataElement>(c => c .Index(indexInfo.Key.Name)); rowsInES += countResponse.Count; } } long rowsInSourceFiles; using (EventLogReader reader = EventLogReader.CreateReader(eventLogSettings.EventLogPath)) rowsInSourceFiles = reader.Count(); Assert.NotEqual(0, rowsInSourceFiles); Assert.NotEqual(0, rowsInES); Assert.Equal(rowsInSourceFiles, rowsInES); }
static void Main() { IConfiguration Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build(); IConfigurationSection eventLogSection = Configuration.GetSection("EventLog"); string eventLogPath = eventLogSection.GetValue("SourcePath", string.Empty); int watchPeriodSeconds = eventLogSection.GetValue("WatchPeriod", 60); int watchPeriodSecondsMs = watchPeriodSeconds * 1000; bool useWatchMode = eventLogSection.GetValue("UseWatchMode", false); int portion = eventLogSection.GetValue("Portion", 1000); IConfigurationSection informationSystemSection = Configuration.GetSection("InformationSystem"); string informationSystemName = informationSystemSection.GetValue("Name", string.Empty); string informationSystemDescription = informationSystemSection.GetValue("Description", string.Empty); IConfigurationSection elasticSearchSection = Configuration.GetSection("ElasticSearch"); Uri nodeAddress = elasticSearchSection.GetValue <Uri>("Node"); string indexName = elasticSearchSection.GetValue <string>("IndexName"); string indexSeparation = elasticSearchSection.GetValue <string>("IndexSeparationPeriod"); int maximumRetries = elasticSearchSection.GetValue <int>("MaximumRetries"); int maxRetryTimeout = elasticSearchSection.GetValue <int>("MaxRetryTimeout"); IConfigurationSection applicationSection = Configuration.GetSection("Application"); bool waitAfterFinish = applicationSection.GetValue("WaitAfterFinish", false); if (string.IsNullOrEmpty(eventLogPath)) { Console.WriteLine("Не указан каталог с файлами данных журнала регистрации."); Console.WriteLine("Для выхода нажмите любую клавишу..."); Console.Read(); return; } Console.WriteLine(); Console.WriteLine(); ConnectionSettings elasticSettings = new ConnectionSettings(nodeAddress) .DefaultIndex(indexName) .MaximumRetries(maximumRetries) .MaxRetryTimeout(TimeSpan.FromSeconds(maxRetryTimeout)); using (EventLogExportMaster exporter = new EventLogExportMaster()) { exporter.SetEventLogPath(eventLogPath); EventLogOnElasticSearch target = new EventLogOnElasticSearch(elasticSettings, portion); target.SetInformationSystem(new InformationSystemsBase() { Name = informationSystemName, Description = informationSystemDescription }); target.SetIndexName(indexName); target.SetIndexSeparationPeriod(indexSeparation); exporter.SetTarget(target); exporter.BeforeExportData += BeforeExportData; exporter.AfterExportData += AfterExportData; exporter.OnErrorExportData += OnErrorExportData; _beginPortionExport = DateTime.Now; if (useWatchMode) { while (true) { if (Console.KeyAvailable) { if (Console.ReadKey().KeyChar == 'q') { break; } } while (exporter.NewDataAvailable()) { exporter.SendData(); } Thread.Sleep(watchPeriodSecondsMs); } } else { while (exporter.NewDataAvailable()) { exporter.SendData(); } } } Console.WriteLine(); Console.WriteLine(); if (waitAfterFinish) { Console.WriteLine("Для выхода нажмите любую клавишу..."); Console.Read(); } }