private bool UpdateBuffer()
        {
            _readBuffer.Clear();
            _lastRowNumberFromBuffer = 0;

            using (_connection = new SQLiteConnection(ConnectionString))
            {
                _connection.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(GetQueryTextForLogData(), _connection))
                {
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            try
                            {
                                if (AddLogRowToBuffer(reader))
                                {
                                    continue;
                                }
                                _currentRow = null;
                                return(false);
                            }
                            catch (Exception ex)
                            {
                                RaiseOnError(new OnErrorEventArgs(ex, reader.GetRowAsString(), false));
                                _currentRow = null;
                            }
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        public void GetRowAsString_Test()
        {
            string connectionString = SQLiteExtensions.GetConnectionString(_sampleDatabaseFile);

            string queryText = String.Format(
                "Select\n" +
                "    el.RowId AS RowId,\n" +
                "    el.Date AS Date,\n" +
                "    el.ConnectId AS ConnectId,\n" +
                "    el.Session AS Session\n" +
                "From\n" +
                "    EventLog el\n" +
                "Where RowID > {0}\n" +
                "Order By rowID\n" +
                "Limit {1}\n", 0, 1);

            string rowAsString = String.Empty;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using SQLiteCommand command = new SQLiteCommand(queryText, connection);
                SQLiteDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    rowAsString = reader.GetRowAsString();
                }
            }
            int countLines = rowAsString.Split('\n').Where(str => str != string.Empty).Count();

            Assert.NotEqual(String.Empty, rowAsString);
            Assert.Equal(4, countLines);
            Assert.Contains("RowId", rowAsString);
            Assert.Contains("Date", rowAsString);
            Assert.Contains("ConnectId", rowAsString);
            Assert.Contains("Session", rowAsString);
        }
Esempio n. 3
0
        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);
            }
        }