-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogTail.cs
97 lines (86 loc) · 3.05 KB
/
LogTail.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace LogReader
{
using System;
using System.IO;
using System.Threading;
public class LogTail : IDisposable
{
/// <summary>
/// Reads the lines from our fileStream
/// </summary>
private readonly StreamReader streamReader;
/// <summary>
/// Constructor to use a file and seek to the end of that file to start reading the tail of a file.
/// </summary>
/// <param name="logPath">The path the the file you wish to open.</param>
public LogTail(string logPath)
: this(new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
}
/// <summary>
/// Constructor to use a fileStream and seek to the end of that file stream to start reading the tail of a file.
/// </summary>
/// <param name="fileStream"></param>
public LogTail(FileStream fileStream)
{
// Seek to the end of the file
fileStream.Seek(0, SeekOrigin.End);
// Open the stream reader...
this.streamReader = new StreamReader(fileStream);
}
/// <summary>
/// Event to fire a line string back to the main form class
/// </summary>
public event Action<string> OutputLine = delegate { };
/// <summary>
/// Is the log file currently being read line by line
/// </summary>
public bool IsReading { get; private set; }
/// <summary>
/// Dispose of the object.
/// </summary>
public void Dispose()
{
if (this.streamReader != null)
{
this.streamReader.Close();
this.streamReader.Dispose();
}
}
/// <summary>
/// Start the log tail.
/// </summary>
public void Start()
{
ThreadPool.QueueUserWorkItem(
delegate
{
// If we are not currently reading...
if (!this.IsReading)
{
// Set the flag that we are reading
this.IsReading = true;
// While we are allowed to read...
while (this.IsReading)
{
while (!this.streamReader.EndOfStream && this.IsReading)
{
this.OutputLine?.Invoke(this.streamReader.ReadLine());
}
while (this.streamReader.EndOfStream && this.IsReading)
{
Thread.Sleep(100);
}
}
}
});
}
/// <summary>
/// Stop the log tail.
/// </summary>
public void Stop()
{
this.IsReading = false;
}
}
}