So I had to write an application that read the logs from other applications.
Something so simple, yet couldn’t find a solution I was happy with.
Still not 100% happy with it because if multiple lines are written at the same time it would only get the last line written.
But here is what I came up with.
Imports System.IO
Public Class LogMonitor
Private WithEvents watch__1 As FileSystemWatcher = New FileSystemWatcher()
Public Event NewLineReceived(Data As String, FileName As String)
Public Sub New(Dir As String, Optional Filter As String = "*.*")
watch__1.Path = Dir
watch__1.Filter = Filter
watch__1.NotifyFilter = NotifyFilters.LastWrite
watch__1.EnableRaisingEvents = True
End Sub
Private Sub watch__1_Changed(sender As Object, e As FileSystemEventArgs) Handles watch__1.Changed
Try
Dim strChangedText As String = GetLastLine(e.FullPath)
RaiseEvent NewLineReceived(strChangedText, e.FullPath)
watch__1.EnableRaisingEvents = False
Finally
watch__1.EnableRaisingEvents = True
End Try
End Sub
Public Function GetLastLine(filepath As String) As String
Dim SR As New StreamReader(File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Dim LastLine As String
Do
LastLine = SR.ReadLine
Loop While SR.EndOfStream = False
SR.Close()
Return LastLine
End Function
End Class






