eventlog monitoring

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

I am trying to write an eventlog monitor. I am using the sample code
provided by VB2005 SDK as the bases for my application. I need to monitor
all three logs (application, system, and security). How can I monitor all
three logs at once?

Thanks,
 
Ben,

Create an eventlog class for each of the logs. Handle the
entrywritten event for each of the variables.

http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.entrywritten.aspx

Dim WithEvents evtApp As New EventLog("Applcation")
Dim WithEvents evtSys As New EventLog("System")
Dim WithEvents evtSec As New EventLog("Security")


Private Sub evtApp_EntryWritten(ByVal sender As Object, ByVal e As
System.Diagnostics.EntryWrittenEventArgs) Handles evtApp.EntryWritten,
evtSec.EntryWritten, evtSys.EntryWritten

End Sub


Ken
 
My last experience with VB was VB3. I am using Visual Studio 2005 now and
things are very different.
I have taken the code you provided and put it in the program and now it runs
but does not report anything. Is this what you meant with your suggestion?

Thanks,


Imports System
Imports System.Environment
Imports System.Diagnostics
Imports System.Threading

Namespace Microsoft.Samples
Module LogMonitor

Public Event EntryWritten As EntryWrittenEventHandler

Dim WithEvents evtApp As New EventLog("Application")
Dim WithEvents evtSys As New EventLog("System")
Dim WithEvents evtSec As New EventLog("Security")
Private signal As AutoResetEvent

Public Sub Main()
Dim log As String
Dim aLog As EventLog
Dim machine As String = "."

log = "system"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The sytem log does not exist!", , "Error")
Exit Sub
End If

log = "application"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The application log does not exist!", , "Error")
Exit Sub
End If

log = "security"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The security log does not exist!", , "Error")
Exit Sub
End If

aLog = New EventLog
aLog.MachineName = machine
AddHandler aLog.EntryWritten, AddressOf OnEntryWritten
' aLog.EnableRaisingEvents = True This causes the
program to crash on my PC
signal = New AutoResetEvent(False)
signal.WaitOne()
End Sub

Sub OnEntryWritten(ByVal source As Object, ByVal e As
System.Diagnostics.EntryWrittenEventArgs) Handles evtApp.EntryWritten,
evtSec.EntryWritten, evtSys.EntryWritten
MsgBox(e.Entry.Message, , "Eventlog Monitor")
End Sub

Private Sub evtApp_EntryWritten(ByVal sender As Object, ByVal e As
System.Diagnostics.EntryWrittenEventArgs) Handles evtApp.EntryWritten,
evtSec.EntryWritten, evtSys.EntryWritten
End Sub

End Module

End Namespace
 
Is this what you meant?

Thanks,
Ben

'-----------------------------------------------------------------------
' This file is part of the Microsoft .NET Framework SDK Code Samples.
'
' Copyright (C) Microsoft Corporation. All rights reserved.
'
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation. See these other
'materials for detailed information regarding Microsoft code samples.
'
'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'-----------------------------------------------------------------------
Imports System
Imports System.Environment
Imports System.Diagnostics
Imports System.Threading
Namespace Microsoft.Samples
Module LogMonitor
Public Event EntryWritten As EntryWrittenEventHandler
Dim WithEvents evtApp As New EventLog("Application")
Dim WithEvents evtSys As New EventLog("System")
Dim WithEvents evtSec As New EventLog("Security")

Public Sub Main()
Dim log As String
Dim aLog As EventLog
Dim machine As String = "."

log = "system"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The sytem log does not exist!", , "Error")
Exit Sub
End If
log = "application"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The application log does not exist!", , "Error")
Exit Sub
End If
log = "security"
If (Not EventLog.Exists(log, machine)) Then
MsgBox("The security log does not exist!", , "Error")
Exit Sub
End If

aLog = New EventLog
aLog.MachineName = machine

AddHandler aLog.EntryWritten, AddressOf OnEntryWritten
End Sub

Sub OnEntryWritten(ByVal source As Object, ByVal e As
System.Diagnostics.EntryWrittenEventArgs) Handles evtApp.EntryWritten,
evtSec.EntryWritten, evtSys.EntryWritten
MsgBox(e.Entry.Message, , "Eventlog Monitor")
End Sub

End Module

End Namespace
 
Back
Top