private void FindNearestBeginEventPosition(ref bool isCorrectBeginEvent, string currentFilePath, ref long newStreamPosition, int stepSize = 1)
        {
            int attemptToFoundBeginEventLine = 0;

            while (!isCorrectBeginEvent && attemptToFoundBeginEventLine < 10)
            {
                string beginEventLine;
                using (FileStream fileStreamCheckPosition =
                           new FileStream(currentFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    fileStreamCheckPosition.Seek(newStreamPosition, SeekOrigin.Begin);
                    using (StreamReader fileStreamCheckReader = new StreamReader(fileStreamCheckPosition))
                        beginEventLine = fileStreamCheckReader.ReadLineWithoutNull();
                }

                if (beginEventLine == null)
                {
                    isCorrectBeginEvent = false;
                    break;
                }

                isCorrectBeginEvent = LogParserTechJournal.ItsBeginOfEvent(beginEventLine);
                if (!isCorrectBeginEvent)
                {
                    newStreamPosition            -= stepSize;
                    attemptToFoundBeginEventLine += 1;
                }
            }
        }
        public bool GoToEvent(long eventNumber)
        {
            Reset();

            int  fileIndex          = -1;
            long currentLineNumber  = -1;
            long currentEventNumber = 0;
            bool moved = false;

            foreach (string logFile in _logFilesWithData)
            {
                fileIndex        += 1;
                currentLineNumber = -1;

                IEnumerable <string> allLines = File.ReadLines(logFile);
                foreach (string line in allLines)
                {
                    currentLineNumber += 1;
                    if (LogParserTechJournal.ItsBeginOfEvent(line))
                    {
                        currentEventNumber += 1;
                    }

                    if (currentEventNumber == eventNumber)
                    {
                        moved = true;
                        break;
                    }
                }

                if (currentEventNumber == eventNumber)
                {
                    moved = true;
                    break;
                }
            }

            if (moved && fileIndex >= 0 && currentLineNumber >= 0)
            {
                InitializeStream(currentLineNumber, fileIndex);
                _eventCount             = eventNumber - 1;
                _currentFileEventNumber = eventNumber;

                return(true);
            }
            else
            {
                return(false);
            }
        }
        private long GetEventCount()
        {
            long eventCount = 0;

            foreach (var logFile in _logFilesWithData)
            {
                using (StreamReader logFileStream = new StreamReader(File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
                {
                    do
                    {
                        string logFileCurrentString = logFileStream.ReadLineWithoutNull();
                        if (LogParserTechJournal.ItsBeginOfEvent(logFileCurrentString))
                        {
                            eventCount++;
                        }
                    } while (!logFileStream.EndOfStream);
                }
            }

            return(eventCount);
        }
        public bool Read()
        {
            bool output;

            try
            {
                if (!InitializeReadFileStream())
                {
                    return(false);
                }

                RaiseBeforeReadFileEvent(out bool cancelBeforeReadFile);
                if (cancelBeforeReadFile)
                {
                    NextFile();
                    return(Read());
                }

                while (true)
                {
                    string sourceData = ReadSourceDataFromStream();
                    if (sourceData != null)
                    {
                        AddNewLineToSource(sourceData, true);
                    }

                    if (LogParserTechJournal.ItsEndOfEvent(_stream, sourceData) || sourceData == null)
                    {
                        _currentFileEventNumber += 1;
                        string preparedSourceData = _eventSource.ToString();
                        _eventSource.Clear();

                        RaiseBeforeRead(new BeforeReadEventArgs(preparedSourceData, _currentFileEventNumber));

                        if (sourceData == null && preparedSourceData.Length == 0)
                        {
                            NextFile();
                            output = Read();
                            break;
                        }

                        try
                        {
                            RowData eventData = ReadRowData(preparedSourceData);
                            _currentRow = eventData;
                            RaiseAfterRead(new AfterReadEventArgs(_currentRow, _currentFileEventNumber));
                            output = true;
                            break;
                        }
                        catch (Exception ex)
                        {
                            RaiseOnError(new OnErrorEventArgs(ex, preparedSourceData, false));
                            _currentRow = null;
                            output      = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                RaiseOnError(new OnErrorEventArgs(ex, null, true));
                _currentRow = null;
                output      = false;
            }

            return(output);
        }
        private RowData ReadRowData(string sourceData)
        {
            RowData eventData = LogParserTechJournal.Parse(sourceData, CurrentFile);

            return(eventData);
        }
Exemple #6
0
        public bool Read()
        {
            bool output;

            try
            {
                if (!InitializeReadFileStream())
                {
                    return(false);
                }

                RaiseBeforeReadFileEvent(out bool cancelBeforeReadFile);
                if (cancelBeforeReadFile)
                {
                    NextFile();
                    return(Read());
                }

                bool     newLine      = true;
                DateTime maxLogPeriod = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, _logTimeZoneInfo).AddMilliseconds((-1) * _readDelayMs);
                _currentStreamPosition = _stream?.GetPosition() ?? 0;

                while (true)
                {
                    if (_firstReadForFile)
                    {
                        _sourceData       = _stream.ReadLineWithoutNull();
                        _firstReadForFile = false;
                    }

                    if (_sourceData == null)
                    {
                        NextFile();
                        output = Read();
                        break;
                    }

                    AddNewLineToSource(_sourceData, newLine);

                    long currentStreamPositionBeforeGoAheadRead = _stream?.GetPosition() ?? 0;
                    if (LogParserTechJournal.ItsEndOfEvent(_stream, CurrentFile, out _sourceData))
                    {
                        _currentFileEventNumber += 1;
                        _currentStreamPosition   = currentStreamPositionBeforeGoAheadRead;
                        string preparedSourceData = _eventSource.ToString();
                        _eventSource.Clear();

                        if (_sourceData == null)
                        {
                            _firstReadForFile = true;
                        }

                        RaiseBeforeRead(new BeforeReadEventArgs(preparedSourceData, _currentFileEventNumber));

                        try
                        {
                            EventData eventData = ReadRowData(preparedSourceData);
                            if (eventData.Period >= maxLogPeriod)
                            {
                                _currentRow = null;
                                output      = false;
                                break;
                            }
                            else
                            {
                                _currentRow = eventData;
                                RaiseAfterRead(new AfterReadEventArgs(_currentRow, _currentFileEventNumber));
                                output = true;
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            RaiseOnError(new OnErrorEventArgs(ex, preparedSourceData, false));
                            _currentRow = null;
                            output      = true;
                            break;
                        }
                    }
                    newLine = false;
                }
            }
            catch (FileNotFoundException)
            {
                _currentRow = null;
                output      = false;
            }
            catch (Exception ex)
            {
                RaiseOnError(new OnErrorEventArgs(ex, null, true));
                _currentRow = null;
                output      = false;
            }

            return(output);
        }
Exemple #7
0
        private EventData ReadRowData(string sourceData)
        {
            EventData eventData = LogParserTechJournal.Parse(sourceData, CurrentFile, _currentFileEventNumber, _logTimeZoneInfo);

            return(eventData);
        }