public void StartWatching() { if (File == null) { return; } bool firstRun = true; long initialFileSize = File.Length; long lastReadLength = initialFileSize - 1024; if (lastReadLength < 0) { lastReadLength = 0; } new Thread(() => { while (true) { long fileSize = new FileInfo(File.FullName).Length; if (fileSize > lastReadLength) { using (FileStream fs = new FileStream(File.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fs.Seek(lastReadLength, SeekOrigin.Begin); byte[] buffer = new byte[1024]; while (true) { int bytesRead = fs.Read(buffer, 0, buffer.Length); if (firstRun) { lastReadLength = fileSize; bytesRead = 0; } else { lastReadLength += bytesRead; } if (bytesRead == 0) { break; } string text = Encoding.ASCII.GetString(buffer, 0, bytesRead); TextWritten.Invoke(this, text); } } } firstRun = false; Thread.Sleep(100); } }) { IsBackground = true }.Start(); }
void OnWrite(string value) { TextWritten?.Invoke(value); if (chainedWriters != null) { foreach (TextWriter cw in chainedWriters) { cw.Write(value); } } }
void OnWrite(char[] buffer, int index, int count) { TextWritten?.Invoke(new string (buffer, index, count)); if (chainedWriters != null) { foreach (TextWriter cw in chainedWriters) { cw.Write(buffer, index, count); } } }
public override void WriteLine(string value, params object[] args) { TextWritten?.Invoke(this, new TextWrittenEventArgs(string.Format(value, args))); _output.WriteLine(value, args); }
public override void WriteLine(string value) { TextWritten?.Invoke(this, new TextWrittenEventArgs(value)); _output.WriteLine(value); }
protected virtual void OnTextWritten() { TextWritten?.Invoke(this, EventArgs.Empty); }