private void ExportToClickHouse(EventLogExportSettings eventLogSettings)
        {
            Console.WriteLine(_settings.ConnectionString);

            ClickHouseHelpers.DropDatabaseIfExist(_settings.ConnectionString);

            EventLogOnClickHouse target = new EventLogOnClickHouse(_settings.ConnectionString, eventLogSettings.Portion);

            target.SetInformationSystem(new InformationSystemsBase()
            {
                Name        = eventLogSettings.InforamtionSystemName,
                Description = eventLogSettings.InforamtionSystemDescription
            });

            ExportHelper.ExportToTargetStorage(eventLogSettings, target);

            long rowsInDB;

            using (var connection = new ClickHouseConnection(_settings.ConnectionString))
            {
                connection.Open();
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText =
                        @"SELECT 
                            COUNT(*) CNT 
                        FROM RowsData rd
                        WHERE InformationSystem = {InformationSystem:String}";
                    cmd.Parameters.Add(new ClickHouseDbParameter
                    {
                        ParameterName = "InformationSystem",
                        Value         = eventLogSettings.InforamtionSystemName
                    });
                    using (var cmdReader = cmd.ExecuteReader())
                    {
                        if (cmdReader.Read())
                        {
                            rowsInDB = Convert.ToInt64(cmdReader.GetValue(0));
                        }
                        else
                        {
                            rowsInDB = 0;
                        }
                    }
                }
            }

            long rowsInSourceFiles;

            using (EventLogReader reader = EventLogReader.CreateReader(eventLogSettings.EventLogPath))
                rowsInSourceFiles = reader.Count();

            Assert.NotEqual(0, rowsInSourceFiles);
            Assert.NotEqual(0, rowsInDB);
            Assert.Equal(rowsInSourceFiles, rowsInDB);
        }
Exemple #2
0
        static void Main()
        {
            IConfiguration Configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                           .Build();

            string connectionString = Configuration.GetConnectionString("EventLogDatabase");

            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);
            string timeZoneName = informationSystemSection.GetValue("TimeZone", string.Empty);

            if (string.IsNullOrEmpty(eventLogPath))
            {
                Console.WriteLine("Не указан каталог с файлами данных журнала регистрации.");
                Console.WriteLine("Для выхода нажмите любую клавишу...");
                Console.Read();
                return;
            }

            Console.WriteLine();
            Console.WriteLine();

            using (EventLogExportMaster exporter = new EventLogExportMaster())
            {
                exporter.SetEventLogPath(eventLogPath);

                EventLogOnClickHouse target = new EventLogOnClickHouse(
                    connectionString,
                    portion,
                    new ClickHouseExtendedActions());
                target.SetInformationSystem(new InformationSystemsBase()
                {
                    Name         = informationSystemName,
                    Description  = informationSystemDescription,
                    TimeZoneName = timeZoneName
                });
                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();
            Console.WriteLine("Для выхода нажмите любую клавишу...");
            Console.Read();
        }