コード例 #1
0
        protected virtual (long recordsRead, long bytesRead) ParseLogFile(string fileName, string fullPath)
        {
            long recordsRead = 0;
            long bytesRead   = 0;

            if (!_logFiles.TryGetValue(fileName, out TContext sourceInfo))
            {
                sourceInfo = CreateLogSourceInfo(fullPath, 0);
                _logFiles.Add(fileName, sourceInfo);
            }
            try
            {
                using (var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    //The responsibility of the following line has been moved to parser in case the parser need to get the meta data before the position
                    //fs.Position = sourceInfo.Position;
                    using (var sr = CreateStreamReader(fs, _encoding))
                    {
                        var records = _recordParser.ParseRecords(sr, sourceInfo);
                        foreach (var record in records)
                        {
                            ILogEnvelope envelope = (ILogEnvelope)record;
                            if (record.Timestamp > (this.InitialPositionTimestamp ?? DateTime.MinValue) &&
                                envelope.LineNumber > _skipLines)
                            {
                                _recordSubject.OnNext(record);
                                recordsRead++;
                            }
                            if (!_started)
                            {
                                break;
                            }
                        }

                        //Need to grab the position before disposing the reader because disposing the reader will dispose the stream
                        bytesRead           = fs.Position - sourceInfo.Position;
                        sourceInfo.Position = fs.Position;
                        sourceInfo.ConsecutiveIOExceptionCount = 0;
                    }
                }
            }
            catch (IOException ex)
            {
                //Add it back to buffer for processing
                AddToBuffer(fileName);
                sourceInfo.ConsecutiveIOExceptionCount++;
                if (sourceInfo.ConsecutiveIOExceptionCount >= this.NumberOfConsecutiveIOExceptionsToLogError)
                {
                    _logger?.LogError(ex.ToMinimized());
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.ToMinimized());
            }
            return(recordsRead, bytesRead);
        }
コード例 #2
0
        protected virtual (long recordsRead, long bytesRead) ParseLogFile(string relativeFilePath, string fullPath)
        {
            long recordsRead = 0;
            long bytesRead   = 0;

            int bookmarkId;

            if (!_logFiles.TryGetValue(relativeFilePath, out TContext sourceInfo))
            {
                sourceInfo = CreateLogSourceInfo(fullPath, 0);
                _logFiles.Add(relativeFilePath, sourceInfo);
                bookmarkId = this.bookmarkOnBufferFlush ? BookmarkManager.RegisterBookmark(this.GetBookmarkName(fullPath), 0, (pos) => this.SaveBookmark()).Id : 0;
            }
            else
            {
                bookmarkId = BookmarkManager.GetBookmarkId(this.GetBookmarkName(fullPath));
            }

            try
            {
                using (var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (var sr = CreateStreamReader(fs, _encoding))
                    {
                        var records = _recordParser.ParseRecords(sr, sourceInfo);
                        foreach (var record in records)
                        {
                            ILogEnvelope envelope = (ILogEnvelope)record;
                            if (envelope != null &&
                                record.Timestamp > (this.InitialPositionTimestamp ?? DateTime.MinValue) &&
                                envelope.LineNumber > _skipLines)
                            {
                                record.BookmarkId = bookmarkId;
                                _recordSubject.OnNext(record);
                                recordsRead++;
                            }

                            if (!_started)
                            {
                                break;
                            }
                        }

                        //Need to grab the position before disposing the reader because disposing the reader will dispose the stream
                        bytesRead           = fs.Position - sourceInfo.Position;
                        sourceInfo.Position = fs.Position;
                        sourceInfo.ConsecutiveIOExceptionCount = 0;
                    }
            }
            catch (IOException ex)
            {
                //Add it back to buffer for processing
                AddToBuffer(relativeFilePath);
                sourceInfo.ConsecutiveIOExceptionCount++;
                if (sourceInfo.ConsecutiveIOExceptionCount >= this.NumberOfConsecutiveIOExceptionsToLogError)
                {
                    _logger?.LogError(ex.ToMinimized());
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.ToMinimized());
            }
            return(recordsRead, bytesRead);
        }