K
Kai Thorsrud
Hi i'm an event Noob and i'm calling a class from my main module console app
like this. I'm sorry if this is a lot of code to read but i can't see the
error according to my book.
Thanks a LOT.
if there are other noobs here like me feel free to use my code if it's of
any use
I've set a breakpoint at doWorkOnChangedFile but notinh happens... i'm
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now for
debugging purposes.
'-----------------------------------------
Class sysLogIoHandler code
'-----------------------------------------
Public Sub Constructor()
Dim oFileStream As System.IO.File
' Ensure that the file supports the properties we are to use later
on and then set the EOF position of the File
' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName
If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not
exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If
' Try to open a FileStream
Try
oWatcherFileSystemStream = (oFileStream.Open(sCompleteFileName,
FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try
' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream
does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If
'Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()
End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)
End Function
Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
'Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
'Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
'Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
'Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile
' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True
End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub
Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As
System.Io.FileSystemEventArgs)
End Sub
Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A 'Changed File
Event' Triggered and I'm trying to find the change but the filesize is the
same as lasttime i did this" & , EventLogEntryType.Error)
Return "" ' Return with no Data
End If
Dim iNumberOfBytes As Integer
' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF, SeekOrigin.Begin)
'Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes)
' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function
like this. I'm sorry if this is a lot of code to read but i can't see the
error according to my book.
Thanks a LOT.
if there are other noobs here like me feel free to use my code if it's of
any use
I've set a breakpoint at doWorkOnChangedFile but notinh happens... i'm
flooding my log like hell.
Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop
And here is some code from my class. I call the Sub Constructor now for
debugging purposes.
'-----------------------------------------
Class sysLogIoHandler code
'-----------------------------------------
Public Sub Constructor()
Dim oFileStream As System.IO.File
' Ensure that the file supports the properties we are to use later
on and then set the EOF position of the File
' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName
If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not
exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If
' Try to open a FileStream
Try
oWatcherFileSystemStream = (oFileStream.Open(sCompleteFileName,
FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try
' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream
does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If
'Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()
End Sub
Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)
End Function
Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
'Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
'Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
'Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
'Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile
' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True
End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub
Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As
System.Io.FileSystemEventArgs)
End Sub
Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A 'Changed File
Event' Triggered and I'm trying to find the change but the filesize is the
same as lasttime i did this" & , EventLogEntryType.Error)
Return "" ' Return with no Data
End If
Dim iNumberOfBytes As Integer
' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF, SeekOrigin.Begin)
'Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes)
' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function