public override long Count() { if (_eventCount < 0) { using (_connection = new SQLiteConnection(ConnectionString)) { _connection.Open(); string queryText = String.Format( "Select\n" + " COUNT(el.RowId) CNT\n" + "From\n" + " EventLog el\n"); SQLiteCommand cmd = new SQLiteCommand(_connection) { CommandType = System.Data.CommandType.Text, CommandText = queryText }; SQLiteDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { _eventCount = reader.GetInt64OrDefault(0); } } } return(_eventCount); }
private long GetLastRowId(long eventNumberToSkip) { long valueLastRowId = 0; using (_connection = new SQLiteConnection(ConnectionString)) { _connection.Open(); string queryText = string.Format( "Select\n" + " el.RowId\n" + "From\n" + " EventLog el\n" + "Where RowID > {0}\n" + "Order By rowID\n" + "Limit 1 OFFSET {1}\n", _lastRowId, eventNumberToSkip); using (SQLiteCommand cmd = new SQLiteCommand(queryText, _connection)) using (SQLiteDataReader reader = cmd.ExecuteReader()) if (reader.Read()) { valueLastRowId = reader.GetInt64OrDefault(0); } } return(valueLastRowId); }
private bool AddLogRowToBuffer(SQLiteDataReader _reader) { DateTime rowPeriod = _reader.GetInt64OrDefault(1).ToDateTimeFormat(); if (rowPeriod >= ReferencesReadDate) { ReadEventLogReferences(); } RowData row = new RowData(); row.FillBySqliteReader(this, _reader); _readBuffer.Add(row); return(true); }
public override bool GoToEvent(long eventNumber) { Reset(); long eventCount = Count(); if (eventCount >= eventNumber) { long eventNumberToSkip = eventNumber - 1; if (eventNumberToSkip <= 0) { _lastRowId = 0; _currentFileEventNumber = 0; return(true); } using (_connection = new SQLiteConnection(ConnectionString)) { _connection.Open(); string queryText = String.Format( "Select\n" + " el.RowId\n" + "From\n" + " EventLog el\n" + "Where RowID > {0}\n" + "Order By rowID\n" + "Limit 1 OFFSET {1}\n", _lastRowId, eventNumberToSkip); using (SQLiteCommand cmd = new SQLiteCommand(queryText, _connection)) { using (SQLiteDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { _lastRowId = reader.GetInt64OrDefault(0); _currentFileEventNumber = eventNumber; return(true); } } } } } return(false); }
public override bool Read() { try { BeforeReadFileEventArgs beforeReadFileArgs = new BeforeReadFileEventArgs(_logFilePath); if (_eventCount < 0) { RaiseBeforeReadFile(beforeReadFileArgs); } if (beforeReadFileArgs.Cancel) { _currentRow = null; return(false); } #region bufferedRead if (_lastRowNumberFromBuffer == 0 || _lastRowNumberFromBuffer >= _readBufferSize) { _readBuffer.Clear(); _lastRowNumberFromBuffer = 0; using (_connection = new SQLiteConnection(ConnectionString)) { _connection.Open(); string queryText = String.Format( "Select\n" + " el.RowId,\n" + " el.Date AS Date,\n" + " el.ConnectId,\n" + " el.Session,\n" + " el.TransactionStatus,\n" + " el.TransactionDate,\n" + " el.TransactionId,\n" + " el.UserCode AS UserCode,\n" + " el.ComputerCode AS ComputerCode,\n" + " el.appCode AS ApplicationCode,\n" + " el.eventCode AS EventCode,\n" + " el.primaryPortCode AS PrimaryPortCode,\n" + " el.secondaryPortCode AS SecondaryPortCode,\n" + " el.workServerCode AS WorkServerCode,\n" + " el.Severity AS SeverityCode,\n" + " el.Comment AS Comment,\n" + " el.Data AS Data,\n" + " el.DataPresentation AS DataPresentation,\n" + " elm.metadataCode AS MetadataCode\n" + "From\n" + " EventLog el\n" + " left join EventLogMetadata elm on el.RowId = elm.eventLogID\n" + " left join MetadataCodes mc on elm.metadataCode = mc.code\n" + "Where RowID > {0}\n" + "Order By rowID\n" + "Limit {1}\n", _lastRowId, _readBufferSize); using (SQLiteCommand cmd = new SQLiteCommand(queryText, _connection)) { using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { try { _readBuffer.Add(new RowData { RowID = reader.GetInt64OrDefault(0), Period = reader.GetInt64OrDefault(1).ToDateTimeFormat(), ConnectId = reader.GetInt64OrDefault(2), Session = reader.GetInt64OrDefault(3), TransactionStatus = GetTransactionStatus(reader.GetInt64OrDefault(4)), TransactionDate = reader.GetInt64OrDefault(5).ToNullableDateTimeELFormat(), TransactionId = reader.GetInt64OrDefault(6), User = GetUserByCode(reader.GetInt64OrDefault(7)), Computer = GetComputerByCode(reader.GetInt64OrDefault(8)), Application = GetApplicationByCode(reader.GetInt64OrDefault(9)), Event = GetEventByCode(reader.GetInt64OrDefault(10)), PrimaryPort = GetPrimaryPortByCode(reader.GetInt64OrDefault(11)), SecondaryPort = GetSecondaryPortByCode(reader.GetInt64OrDefault(12)), WorkServer = GetWorkServerByCode(reader.GetInt64OrDefault(13)), Severity = GetSeverityByCode(reader.GetInt64OrDefault(14)), Comment = reader.GetStringOrDefault(15), Data = reader.GetStringOrDefault(16).FromWin1251ToUTF8(), DataPresentation = reader.GetStringOrDefault(17), Metadata = GetMetadataByCode(reader.GetInt64OrDefault(18)) }); } catch (Exception ex) { RaiseOnError(new OnErrorEventArgs(ex, reader.GetRowAsString(), false)); _currentRow = null; } } } } } } #endregion if (_lastRowNumberFromBuffer >= _readBuffer.Count) { RaiseAfterReadFile(new AfterReadFileEventArgs(_logFilePath)); _currentRow = null; return(false); } RaiseBeforeRead(new BeforeReadEventArgs(null, _eventCount)); _currentRow = _readBuffer .Where(bufRow => bufRow.RowID > _lastRowId) .First(); _lastRowNumberFromBuffer += 1; _lastRowId = _currentRow.RowID; RaiseAfterRead(new AfterReadEventArgs(_currentRow, _eventCount)); return(true); } catch (Exception ex) { RaiseOnError(new OnErrorEventArgs(ex, null, true)); _currentRow = null; return(false); } }
internal void FillBySqliteReader(EventLogLGDReader reader, SQLiteDataReader sqlReader) { DateTime rowPeriod = sqlReader.GetInt64OrDefault(1).ToDateTimeFormat(); RowId = sqlReader.GetInt64OrDefault(0); Period = rowPeriod; ConnectId = sqlReader.GetInt64OrDefault(2); Session = sqlReader.GetInt64OrDefault(3); TransactionStatus = reader.GetTransactionStatus(sqlReader.GetInt64OrDefault(4)); TransactionDate = sqlReader.GetInt64OrDefault(5).ToNullableDateTimeElFormat(); TransactionId = sqlReader.GetInt64OrDefault(6); User = reader.GetUserByCode(sqlReader.GetInt64OrDefault(7)); Computer = reader.GetComputerByCode(sqlReader.GetInt64OrDefault(8)); Application = reader.GetApplicationByCode(sqlReader.GetInt64OrDefault(9)); Event = reader.GetEventByCode(sqlReader.GetInt64OrDefault(10)); PrimaryPort = reader.GetPrimaryPortByCode(sqlReader.GetInt64OrDefault(11)); SecondaryPort = reader.GetSecondaryPortByCode(sqlReader.GetInt64OrDefault(12)); WorkServer = reader.GetWorkServerByCode(sqlReader.GetInt64OrDefault(13)); Severity = reader.GetSeverityByCode(sqlReader.GetInt64OrDefault(14)); Comment = sqlReader.GetStringOrDefault(15); Data = sqlReader.GetStringOrDefault(16).FromWin1251ToUtf8(); DataUuid = GetDataUuid(Data); DataPresentation = sqlReader.GetStringOrDefault(17); Metadata = reader.GetMetadataByCode(sqlReader.GetInt64OrDefault(18)); }
private void ReadRefferences_IfChanged_Test(string eventLogPath) { DateTime lastReadReferencesDateBeforeRead = DateTime.MinValue; DateTime lastReadReferencesDate = DateTime.MinValue; using (EventLogReader reader = EventLogReader.CreateReader(eventLogPath)) { lastReadReferencesDateBeforeRead = reader.ReferencesReadDate; Thread.Sleep(2000); if (reader is EventLogLGFReader) { #region LGF EventLogLGFReader lgfReader = (EventLogLGFReader)reader; using (StreamWriter sw = File.AppendText(lgfReader.CurrentFile)) { string descriptionNewEvent = "Новое событие в процессе чтения!"; DateTime newLogRecordPeriod = DateTime.Now; string newLogRecordPeriodAsString = newLogRecordPeriod.ToString("yyyyMMddHHmmss"); sw.WriteLine(","); sw.WriteLine($"{{{newLogRecordPeriodAsString},N,"); sw.WriteLine($"{{0,0}},1,1,2,2,3,N,\"{descriptionNewEvent}\",3,"); sw.WriteLine($"{{\"S\",\"{descriptionNewEvent}\"}},\"\",1,1,0,2,0,"); sw.WriteLine("{0}"); sw.WriteLine("}"); } #endregion } else if (reader is EventLogLGDReader) { #region LGD string lgdConnectionString = SQLiteExtensions.GetConnectionString(eventLogPath, false); using (SQLiteConnection connection = new SQLiteConnection(lgdConnectionString)) { connection.Open(); string queryText = String.Format( "Select\n" + " el.RowId,\n" + " el.Date AS Date,\n" + " el.ConnectId,\n" + " el.Session,\n" + " el.TransactionStatus,\n" + " el.TransactionDate,\n" + " el.TransactionId,\n" + " el.UserCode AS UserCode,\n" + " el.ComputerCode AS ComputerCode,\n" + " el.appCode AS ApplicationCode,\n" + " el.eventCode AS EventCode,\n" + " el.primaryPortCode AS PrimaryPortCode,\n" + " el.secondaryPortCode AS SecondaryPortCode,\n" + " el.workServerCode AS WorkServerCode,\n" + " el.Severity AS SeverityCode,\n" + " el.Comment AS Comment,\n" + " el.Data AS Data,\n" + " el.DataPresentation AS DataPresentation,\n" + " elm.metadataCode AS MetadataCode\n" + "From\n" + " EventLog el\n" + " left join EventLogMetadata elm on el.RowId = elm.eventLogID\n" + " left join MetadataCodes mc on elm.metadataCode = mc.code\n" + "Where RowID = (SELECT MAX(RowID) from EventLog)\n"); using (SQLiteCommand sqliteCmd = new SQLiteCommand(queryText, connection)) { long RowID = 0, ConnectId = 0, Session = 0, TransactionStatus = 0, TransactionDate = 0, TransactionId = 0, User = 0, Computer = 0, Application = 0, Event = 0, PrimaryPort = 0, SecondaryPort = 0, WorkServer = 0, Severity = 0, Metadata = 0; string Comment = string.Empty, Data = string.Empty, DataPresentation = string.Empty; using (SQLiteDataReader sqliteReader = sqliteCmd.ExecuteReader()) { while (sqliteReader.Read()) { RowID = sqliteReader.GetInt64OrDefault(0); ConnectId = sqliteReader.GetInt64OrDefault(2); Session = sqliteReader.GetInt64OrDefault(3); TransactionStatus = sqliteReader.GetInt64OrDefault(4); TransactionDate = sqliteReader.GetInt64OrDefault(5); TransactionId = sqliteReader.GetInt64OrDefault(6); User = sqliteReader.GetInt64OrDefault(7); Computer = sqliteReader.GetInt64OrDefault(8); Application = sqliteReader.GetInt64OrDefault(9); Event = sqliteReader.GetInt64OrDefault(10); PrimaryPort = sqliteReader.GetInt64OrDefault(11); SecondaryPort = sqliteReader.GetInt64OrDefault(12); WorkServer = sqliteReader.GetInt64OrDefault(13); Severity = sqliteReader.GetInt64OrDefault(14); Comment = sqliteReader.GetStringOrDefault(15); Data = sqliteReader.GetStringOrDefault(16); DataPresentation = sqliteReader.GetStringOrDefault(17); Metadata = sqliteReader.GetInt64OrDefault(18); } } string queryInsertLog = "INSERT INTO EventLog " + "(" + " RowId, " + " Date, " + " ConnectId, " + " Session, " + " TransactionStatus, " + " TransactionDate, " + " TransactionId, " + " UserCode, " + " ComputerCode, " + " appCode, " + " eventCode, " + " primaryPortCode, " + " secondaryPortCode, " + " workServerCode, " + " Severity, " + " Comment, " + " Data, " + " DataPresentation " + ") " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; using (SQLiteCommand insertSQL = new SQLiteCommand(queryInsertLog, connection)) { long newRowId = RowID + 1; long newPeriod = DateTime.Now.ToLongDateTimeFormat(); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, newRowId)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, newPeriod)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, ConnectId)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Session)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionStatus)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionDate)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionId)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, User)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Computer)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Application)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Event)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, PrimaryPort)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, SecondaryPort)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, WorkServer)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Severity)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, Comment)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, Data)); insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, DataPresentation)); insertSQL.ExecuteNonQuery(); } } } #endregion } while (reader.Read()) { ; } lastReadReferencesDate = reader.ReferencesReadDate; } Assert.NotEqual(DateTime.MinValue, lastReadReferencesDate); Assert.NotEqual(DateTime.MinValue, lastReadReferencesDateBeforeRead); Assert.True(lastReadReferencesDateBeforeRead < lastReadReferencesDate); }