Home All Groups Group Topic Archive Search About
Author
27 Apr 2006 6:09 PM
mattie
hey all,

can you code against the event log, for instance if an entry for a specific
user shows up can i popup a message box notifying me that?

thanks,
mj

Author
27 Apr 2006 6:22 PM
RSS
Yes it is possible. Look at using System.Diagnostics;

It has EventLog object.
Show quoteHide quote
"mattie" <mat***@discussions.microsoft.com> wrote in message news:BC314A5A-322C-48C9-B405-E5A2A7A95064@microsoft.com...
> hey all,
>
> can you code against the event log, for instance if an entry for a specific
> user shows up can i popup a message box notifying me that?
>
> thanks,
> mj
Author
28 Apr 2006 3:18 PM
Jeff Waskiewicz
Here is a class I was playing with a while back but never actually finished
because the need disappeared.  I do recall that it worked well enough that I
could sucessfully add messages to the event logs and create custom logs.  It
should get you on your way...

Imports System.Diagnostics
Imports System.IO
Imports Microsoft.Win32

Public Class EventLogger


'--------------------------------------------------------------------------------------------
    'Event Logger
    'Written By: Jeff Waskiewicz
    'Date: 8/12/2003
    '
    'Wraps the System.Diagnostics.EventLog Class for Reading and Writing to
the Windows Event Log. 
    '
    'Revision Notes:
    '
    '

'-------------------------------------------------------------------------------------------------------

    Private strSourceName As String
    Private strLogName As String
    Private strMessageText As String
    Private intMessageID As Integer
    Private shtCategory As Short
    Private MessageEventType As EventLogEntryType
    Private EventLog As New System.Diagnostics.EventLog()

    'Property to hold the Source shown in Event Viewer
    Property EventSource() As String
        Get
            Return strSourceName
        End Get
        Set(ByVal Value As String)
            strSourceName = Value
        End Set
    End Property

    'Property to hold the Name of the Event Log to be written to
    Property LogFileName() As String
        Get
            Return strLogName
        End Get
        Set(ByVal Value As String)
            strLogName = Value
        End Set
    End Property

    'Property to hold the type of message stored as an EventLogEntryType. 
There are 5 types
    'Warning, Information, Error, AuditFailed, and AuditSuccess
    Property MessageType() As EventLogEntryType
        Get
            Return MessageEventType
        End Get
        Set(ByVal Value As EventLogEntryType)
            MessageEventType = Value
        End Set
    End Property

    'Property to hold the Message Text
    Property MessageText() As String
        Get
            Return strMessageText
        End Get
        Set(ByVal Value As String)
            strMessageText = Value
        End Set
    End Property

    'Property to hold the application defined Event ID of the Message
    Property MessageID() As String
        Get
            Return intMessageID
        End Get
        Set(ByVal Value As String)

        End Set
    End Property

    'Property to hold the application defined Category of the Message
    Property MessageCategory()
        Get
            Return shtCategory
        End Get
        Set(ByVal Value)
            shtCategory = Value
        End Set
    End Property

    'Used to Creates a New Event Source and Log File if necessary
    Sub CreateSource()
        If Not EventLog.SourceExists(strSourceName) Then
            EventLog.CreateEventSource(strSourceName, strLogName)
        Else
            EventLog.DeleteEventSource(strSourceName)
            EventLog.CreateEventSource(strSourceName, strLogName)
        End If
    End Sub

    'Used to remove an Event Source
    Sub RemoveSource()
        If EventLog.SourceExists(strSourceName) Then
            EventLog.DeleteEventSource(strSourceName)
        End If
    End Sub

    'Used to Delete a Custom Log File
    Sub DeleteLog()
        If EventLog.SourceExists(strSourceName) Then
            EventLog.Delete(strLogName)
        End If
    End Sub

    'Used to Verify a Source Exists
    Function SourceExists() As Boolean
        SourceExists = EventLog.SourceExists(strSourceName)
    End Function

    'Used to Verify a Source Exists
    Function LogFileExists() As Boolean
        LogFileExists = EventLog.Exists(strLogName)
    End Function

    'Used to Write a Message to the Event Log
    Sub WriteMessage()
        EventLog.Source = strSourceName
        If shtCategory <> 0 Then
            EventLog.WriteEntry(strMessageText, MessageEventType,
intMessageID, shtCategory)
        Else
            EventLog.WriteEntry(strMessageText, MessageEventType,
intMessageID)
        End If
    End Sub

    'Used to Read Messages from Event Log
    Private Function ReadMessages()
        MsgBox("Not Implemented")
    End Function

    Private Sub ClearLog()
        MsgBox("Not Implemented")
    End Sub

End Class