public override void SaveLogPosition(FileInfo logFileInfo, EventLogPosition position) { using (EventLogContext _context = EventLogContext.Create(_databaseOptions, _databaseActions)) _context.SaveLogPosition(_system, logFileInfo, position); _lastEventLogFilePosition = position; }
private void GoToEvent_Test(string eventLogPath) { string dataAfterGoEvent = string.Empty; string dataAfterSetPosition = string.Empty; using (EventLogReader reader = EventLogReader.CreateReader(eventLogPath)) { reader.GoToEvent(5); EventLogPosition eventPosition = reader.GetCurrentPosition(); if (reader.Read()) { dataAfterGoEvent = reader.CurrentRow.Data; } reader.Reset(); reader.SetCurrentPosition(eventPosition); if (reader.Read()) { dataAfterSetPosition = reader.CurrentRow.Data; } } Assert.Equal(dataAfterGoEvent, dataAfterSetPosition); }
private void GetAndSetPosition_Test(string eventLogPath) { long countRecords = 0; long countRecordsStepByStep = 0; long countRecordsStepByStepAfterSetPosition = 0; using (EventLogReader reader = EventLogReader.CreateReader(eventLogPath)) { countRecords = reader.Count(); while (reader.Read()) { countRecordsStepByStep += 1; } reader.Reset(); EventLogPosition position = reader.GetCurrentPosition(); while (reader.Read()) { ; } reader.SetCurrentPosition(position); while (reader.Read()) { countRecordsStepByStepAfterSetPosition += 1; } } Assert.NotEqual(0, countRecords); Assert.NotEqual(0, countRecordsStepByStep); Assert.NotEqual(0, countRecordsStepByStepAfterSetPosition); Assert.Equal(countRecords, countRecordsStepByStep); Assert.Equal(countRecords, countRecordsStepByStepAfterSetPosition); }
public void SendData() { if (_reader == null || _target == null || _eventLogPath == null) { return; } EventLogPosition lastPosition = _target.GetLastPosition(); _reader.Reset(); _reader.AfterReadFile += EventLogReader_AfterReadFile; _reader.AfterReadEvent += EventLogReader_AfterReadEvent; _reader.OnErrorEvent += EventLogReader_OnErrorEvent; _reader.SetCurrentPosition(lastPosition); long totalReadEvents = 0; while (_reader.Read()) { if (_reader.CurrentRow != null) { totalReadEvents += 1; } if (totalReadEvents >= _portionSize) { break; } } if (_dataToSend.Count > 0) { SendDataCurrentPortion(_reader); } }
public override void SaveLogPosition(FileInfo logFileInfo, EventLogPosition position) { SaveLogFileHistoryElement(logFileInfo, position); SaveLogFileActualElement(logFileInfo, position); _lastEventLogFilePosition = position; }
private void RiseAfterExportData(EventLogPosition currentPosition) { AfterExportDataHandler handlerAfterExportData = AfterExportData; handlerAfterExportData?.Invoke(new AfterExportDataEventArgs() { CurrentPosition = currentPosition }); }
public LogFiles(InformationSystemsBase system, FileInfo logFileInfo, EventLogPosition position) { InformationSystemId = system.Id; FileName = logFileInfo.Name; CreateDate = logFileInfo.CreationTimeUtc; ModificationDate = logFileInfo.LastWriteTimeUtc; LastCurrentFileData = position.CurrentFileData; LastCurrentFileReferences = position.CurrentFileReferences; LastEventNumber = position.EventNumber; LastStreamPosition = position.StreamPosition; }
private void EventLogReader_AfterReadFile(EventLogReader sender, AfterReadFileEventArgs args) { FileInfo _lastEventLogDataFileInfo = new FileInfo(args.FileName); if (_dataToSend.Count >= 0) { SendDataCurrentPortion(sender); } EventLogPosition position = sender.GetCurrentPosition(); _target.SaveLogPosition(_lastEventLogDataFileInfo, position); }
public override void SaveLogPosition(FileInfo logFileInfo, EventLogPosition position) { using (var context = new ClickHouseContext(_connectionString, _extendedActions)) { context.SaveLogPosition(_system, logFileInfo, position); if (_currentStepToClearLogFiles == 0 || _currentStepToClearLogFiles >= _stepsToClearLogFiles) { context.RemoveArchiveLogFileRecords(_system); _currentStepToClearLogFiles = 0; } _currentStepToClearLogFiles += 1; } _lastEventLogFilePosition = position; }
private void SaveLogFileActualElement(FileInfo logFileInfo, EventLogPosition position) { _client.SaveLogFileActualElement(new LogFileElement() { Id = _system.Name, CreateDate = logFileInfo.CreationTimeUtc, FileName = logFileInfo.Name, InformationSystem = _system.Name, LastCurrentFileData = position.CurrentFileData, LastCurrentFileReferences = position.CurrentFileReferences, LastEventNumber = position.EventNumber, LastStreamPosition = position.StreamPosition, ModificationDate = logFileInfo.LastWriteTimeUtc }, _indexName); }
public override EventLogPosition GetLastPosition() { if (_lastEventLogFilePosition != null) { return(_lastEventLogFilePosition); } EventLogPosition position; using (EventLogContext _context = EventLogContext.Create(_databaseOptions, _databaseActions)) position = _context.GetLastPosition(_system); _lastEventLogFilePosition = position; return(position); }
public override EventLogPosition GetLastPosition() { if (_lastEventLogFilePosition != null) { return(_lastEventLogFilePosition); } EventLogPosition position; using (var context = new ClickHouseContext(_connectionString)) position = context.GetLogFilePosition(_system); _lastEventLogFilePosition = position; return(position); }
public EventLogPosition GetLogFilePosition(InformationSystemsBase system) { var cmdGetLastLogFileInfo = _connection.CreateCommand(); cmdGetLastLogFileInfo.CommandText = @"SELECT LastEventNumber, LastCurrentFileReferences, LastCurrentFileData, LastStreamPosition FROM LogFiles AS LF WHERE InformationSystem = {informationSystem:String} AND Id IN ( SELECT MAX(Id) LastId FROM LogFiles AS LF_LAST WHERE LF_LAST.InformationSystem = {informationSystem:String} )"; cmdGetLastLogFileInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "informationSystem", DbType = DbType.AnsiString, Value = system.Name }); EventLogPosition output = null; using (var cmdReader = cmdGetLastLogFileInfo.ExecuteReader()) { if (cmdReader.Read()) { string fileData = cmdReader.GetString(2) .Replace("\\\\", "\\") .FixNetworkPath(); string fileReferences = cmdReader.GetString(1) .Replace("\\\\", "\\") .FixNetworkPath(); output = new EventLogPosition( cmdReader.GetInt64(0), fileReferences, fileData, cmdReader.GetInt64(3)); } } return(output); }
public bool NewDataAvailable() { if (_reader == null) { return(false); } if (_target == null) { return(false); } if (_eventLogPath == null) { return(false); } EventLogPosition lastPosition = _target.GetLastPosition(); bool newDataExist; _reader.AfterReadFile -= EventLogReader_AfterReadFile; _reader.AfterReadEvent -= EventLogReader_AfterReadEvent; _reader.OnErrorEvent -= EventLogReader_OnErrorEvent; _reader.Reset(); // В случае, если каталог последней позиции не совпадает // с текущим каталогом данных, то предыдущую позицию не учитываем if (lastPosition != null) { FileInfo lastDataFileInfo = new FileInfo(lastPosition.CurrentFileReferences); FileInfo currentDataFileInfo = new FileInfo(_reader.CurrentFile); if (lastDataFileInfo.Directory != null && currentDataFileInfo.Directory != null) { if (lastDataFileInfo.Directory.FullName != currentDataFileInfo.Directory.FullName) { lastPosition = null; } } } _reader.SetCurrentPosition(lastPosition); newDataExist = _reader.Read(); return(newDataExist); }
public override EventLogPosition GetLastPosition() { if (_lastEventLogFilePosition != null) { return(_lastEventLogFilePosition); } LogFileElement actualLogFileInfo = _client.GetLastLogFileElement(_system.Name, _indexName); EventLogPosition position = null; if (actualLogFileInfo != null) { position = new EventLogPosition( actualLogFileInfo.LastEventNumber, actualLogFileInfo.LastCurrentFileReferences, actualLogFileInfo.LastCurrentFileData, actualLogFileInfo.LastStreamPosition ); } _lastEventLogFilePosition = position; return(position); }
public virtual void SaveLogPosition(FileInfo logFileInfo, EventLogPosition position) { throw new NotImplementedException(); }
public void SaveLogPosition(InformationSystemsBase system, FileInfo logFileInfo, EventLogPosition position) { using (ClickHouseBulkCopy bulkCopyInterface = new ClickHouseBulkCopy(_connection) { DestinationTableName = "LogFiles", BatchSize = 100000 }) { long logFileNewId = GetLogFileInfoNewId(system); IEnumerable <object[]> values = new List <object[]>() { new object[] { system.Name, logFileNewId, logFileInfo.Name, logFileInfo.CreationTimeUtc, logFileInfo.LastWriteTimeUtc, position.EventNumber, position.CurrentFileReferences.Replace("\\", "\\\\"), position.CurrentFileData.Replace("\\", "\\\\"), position.StreamPosition ?? 0 } }.AsEnumerable(); var bulkResult = bulkCopyInterface.WriteToServerAsync(values); bulkResult.Wait(); } }
public static void SaveLogPosition(this EventLogContext context, InformationSystemsBase system, FileInfo logFileInfo, EventLogPosition position) { LogFiles foundLogFile = context.LogFiles .FirstOrDefault(l => l.InformationSystemId == system.Id && l.FileName == logFileInfo.Name && l.CreateDate == logFileInfo.CreationTimeUtc); if (foundLogFile == null) { context.LogFiles.Add(new LogFiles(system, logFileInfo, position)); } else { foundLogFile.ModificationDate = logFileInfo.LastWriteTimeUtc; foundLogFile.LastCurrentFileData = position.CurrentFileData; foundLogFile.LastCurrentFileReferences = position.CurrentFileReferences; foundLogFile.LastEventNumber = position.EventNumber; foundLogFile.LastStreamPosition = position.StreamPosition; context.Entry(foundLogFile).State = EntityState.Modified; } context.SaveChanges(); }
public void SaveLogPosition(InformationSystemsBase system, FileInfo logFileInfo, EventLogPosition position) { var commandAddLogInfo = _connection.CreateCommand(); commandAddLogInfo.CommandText = @"INSERT INTO LogFiles ( InformationSystem, Id, FileName, CreateDate, ModificationDate, LastEventNumber, LastCurrentFileReferences, LastCurrentFileData, LastStreamPosition ) VALUES ( {isId:String}, {newId:Int64}, {FileName:String}, {CreateDate:DateTime}, {ModificationDate:DateTime}, {LastEventNumber:Int64}, {LastCurrentFileReferences:String}, {LastCurrentFileData:String}, {LastStreamPosition:Int64} )"; commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "isId", DbType = DbType.Int64, Value = system.Name }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "newId", DbType = DbType.Int64, Value = GetLogFileInfoNewId(system) }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "FileName", DbType = DbType.AnsiString, Value = logFileInfo.Name }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "CreateDate", DbType = DbType.DateTime, Value = logFileInfo.CreationTimeUtc }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "ModificationDate", DbType = DbType.DateTime, Value = logFileInfo.LastWriteTimeUtc }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "LastEventNumber", DbType = DbType.Int64, Value = position.EventNumber }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "LastCurrentFileReferences", DbType = DbType.AnsiString, Value = position.CurrentFileReferences.Replace("\\", "\\\\") }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "LastCurrentFileData", DbType = DbType.AnsiString, Value = position.CurrentFileData.Replace("\\", "\\\\") }); commandAddLogInfo.Parameters.Add(new ClickHouseDbParameter { ParameterName = "LastStreamPosition", DbType = DbType.Int64, Value = position?.StreamPosition ?? 0 }); commandAddLogInfo.ExecuteNonQuery(); }