public void TestReadLine5() { _writer.Write("Foo\r\n"); _writer.Write("Bar"); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be("Foo\r\n"); reader.ReadLine().Should().Be("Bar"); }
public void TestReadLine6() { var builder = new StringBuilder(); builder.Append('a', 2049); var line = builder.ToString(); _writer.Write(line); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be(line); reader.ReadLine().Should().Be(null); }
public void TestReadBigLine1() { var fileName = TextLogSourceAcceptanceTest.File1Mb_1Line; var actualLines = File.ReadAllLines(fileName); using (var stream = File.OpenRead(fileName)) using (var reader = new StreamReaderEx(stream, Encoding.Default)) { var line1 = reader.ReadLine(); line1.Should().EndWith("\n"); var trimmedLine1 = line1.TrimEnd(); trimmedLine1.Equals(actualLines[0]).Should().BeTrue(); //< Using Should().Be(..) crashes VS because it's unable to print 2 Mb //line1.TrimEnd().Should().Be(actualLines[0]); reader.ReadLine().Should().BeNull("because there's no more lines"); } }
public void TestReadLine7() { var builder = new StringBuilder(); builder.Append('a', 2049); builder.Append("\r\n"); var line1 = builder.ToString(); _writer.Write(line1); _writer.Write("Foobar"); _writer.Flush(); _stream.Position = 0; var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().Be(line1); reader.ReadLine().Should().Be("Foobar"); reader.ReadLine().Should().BeNull(); }
/// <inheritdoc /> protected override TimeSpan RunOnce(CancellationToken token) { bool read = false; try { if (!File.Exists(_fileName)) { SetDoesNotExist(); } else { var info = new FileInfo(_fileName); _properties.SetValue(LogFileProperties.LastModified, info.LastWriteTime); _properties.SetValue(LogFileProperties.Created, info.CreationTime); _properties.SetValue(LogFileProperties.Size, Size.FromBytes(info.Length)); using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var format = _properties.GetValue(LogFileProperties.Format); if (format == null) { format = TryFindFormat(stream); _properties.SetValue(LogFileProperties.Format, format); } var encoding = format?.Encoding ?? _encoding; _properties.SetValue(LogFileProperties.Encoding, encoding); using (var reader = new StreamReaderEx(stream, encoding)) { // We change the error flag explicitly AFTER opening // the stream because that operation might throw if we're // not allowed to access the file (in which case a different // error must be set). _properties.SetValue(LogFileProperties.EmptyReason, ErrorFlags.None); if (stream.Length >= _lastPosition) { stream.Position = _lastPosition; } else { OnReset(stream, out _numberOfLinesRead, out _lastPosition); } string currentLine; while ((currentLine = reader.ReadLine()) != null) { token.ThrowIfCancellationRequested(); ResetEndOfSourceReached(); LevelFlags level = LogLine.DetermineLevelFromLine(currentLine); bool lastLineHadNewline = _lastLineHadNewline; var trimmedLine = currentLine.TrimNewlineEnd(out _lastLineHadNewline); var entryCount = _entries.Count; if (entryCount > 0 && !lastLineHadNewline) { // We need to remove the last line and create a new line // that consists of the entire content. RemoveLast(); trimmedLine = _untrimmedLastLine + trimmedLine; _untrimmedLastLine = _untrimmedLastLine + currentLine; } else { _untrimmedLastLine = currentLine; ++_numberOfLinesRead; read = true; } var timestamp = ParseTimestamp(trimmedLine); Add(trimmedLine, level, _numberOfLinesRead, timestamp); } _lastPosition = stream.Position; } } Listeners.OnRead(_numberOfLinesRead); SetEndOfSourceReached(); } } catch (FileNotFoundException e) { SetError(ErrorFlags.SourceDoesNotExist); Log.Debug(e); } catch (DirectoryNotFoundException e) { SetError(ErrorFlags.SourceDoesNotExist); Log.Debug(e); } catch (OperationCanceledException e) { Log.Debug(e); } catch (UnauthorizedAccessException e) { SetError(ErrorFlags.SourceCannotBeAccessed); Log.Debug(e); } catch (IOException e) { SetError(ErrorFlags.SourceCannotBeAccessed); Log.Debug(e); } catch (Exception e) { Log.Debug(e); } if (read) { return(TimeSpan.Zero); } return(TimeSpan.FromMilliseconds(100)); }
public void TestReadLine1() { var reader = new StreamReaderEx(_stream, Encoding.UTF8); reader.ReadLine().Should().BeNull("because the source stream is empty"); }