FileLogTail
ConsoleMonster.Live.Tails
A synchronous, caller-polled reader of completed literal lines from a file that may be rotated.
Inheritance
- object
- FileLogTail
Remarks
Owns its file handle and uses ReadWrite/Delete sharing. No timers, threads or filesystem watchers are created. Calls, including disposal, must be serialized by the caller. UTF-8 is the default; UTF-8, UTF-16 and UTF-32 BOMs override the supplied encoding, including a BOM split between polls. Decoder state survives split writes. CR, LF and CRLF terminate lines; CR emits immediately and a following LF is consumed even across polls. Unterminated text is retained until a terminator arrives, and is discarded on reset or disposal. Rotation follows the path, not the renamed file: unread bytes in the old file are not drained. Identity changes, observed shrinkage and changes in the last 64 consumed bytes reset decoding and resume at byte zero. A truncate/regrow entirely between polls with identical checkpoint bytes is indistinguishable from an append. Concurrent rewrites are not atomic snapshots. Initialisation scans the opening length once, retaining only the requested completed lines plus any unfinished line; each later poll has both a byte cap and a fixed length boundary. File identity uses the operating system on Windows and on x64/arm64 Linux/macOS; other Unix ABIs are unsupported.
Constructors
public FileLogTail(string path, int initialLineCount = 10, Encoding? encoding = null, int maxBytesPerRead = 65536) #Opens the file and captures its last completed lines at the current length.
Parameters
- path string
- The file to follow; it must exist when this reader is constructed.
- initialLineCount int = 10
- Completed history lines to return on the first poll; defaults to 10. Zero skips completed history.
- encoding System.Text.Encoding? = null
- Encoding for files without a BOM, or null for UTF-8. The encoding is copied.
- maxBytesPerRead int = 65536
- Maximum newly consumed bytes per poll; defaults to 65,536 and must be positive. Initial history is separate.
Exceptions
ArgumentNullException– path is null.ArgumentOutOfRangeException– A line count is negative or the byte cap is not positive.IOException– The file cannot be opened or read.PlatformNotSupportedException– The platform's file identity ABI is unsupported.
Methods
public void Dispose() #Closes the owned handle and discards any queued history or unfinished line. Repeated calls are harmless.
public IReadOnlyList<TailedLogEntry> ReadAvailable() #Returns initial history once, then completed lines from a bounded snapshot of newly available bytes.
Returns
IReadOnlyList<TailedLogEntry> Literal parsed entries in file order, or an empty list when no completed lines are available.Exceptions
ObjectDisposedException– This reader has been disposed.IOException– The file cannot be read or identified.