Sunday, July 11, 2010

EventLog Manager Class

This class can be used to view/add/remove/clear event logs on a remote or local system.
An EventLogEntry can also be written.

'EventLogManager.vb
 
Imports System.Diagnostics
 
Public Class EventLogManager
 
 
    Public Sub New(ByVal machineName As String)
        Me.MachineName = machineName
        GetEventLogs()
    End Sub
 
    Private strMachineName As String
    Public Property MachineName() As String
        Get
            Return strMachineName
        End Get
        Set(ByVal value As String)
            strMachineName = value
        End Set
    End Property
 
    Private dicEventLogs As New Dictionary(Of String, EventLog)
    Public Property EventLogs() As Dictionary(Of String, EventLog)
        Get
            Return dicEventlogs
        End Get
        Set(ByVal value As Dictionary(Of String, EventLog))
            dicEventlogs = value
        End Set
    End Property
 
    Private dicEventLogObjects As New Dictionary(Of String, EventLogObject)
    Public Property EventLogObjects() As Dictionary(Of String, EventLogObject)
        Get
            Return dicEventLogObjects
        End Get
        Set(ByVal value As Dictionary(Of String, EventLogObject))
            dicEventLogObjects = value
        End Set
    End Property
 
    Private Sub GetEventLogs()
        Try
            Me.EventLogs.Clear()
            Dim aEventLogs As EventLog() = EventLog.GetEventLogs(Me.MachineName)
            For Each Log As EventLog In aEventLogs
                If Not Me.EventLogs.ContainsKey(Log.Log) Then
                    Me.EventLogs.Add(Log.Log, Log)
                Else
                    Me.EventLogs.Remove(Log.Log)
                    Me.EventLogs.Add(Log.Log, Log)
                End If
                If Not Me.EventLogObjects.ContainsKey(Log.Log) Then
                    Dim EventObj As New EventLogObject(Me, Log.Log, Log.Source)
                    Me.EventLogObjects.Add(Log.Log, EventObj)
                Else
                    Me.EventLogObjects.Remove(Log.Log)
                    Dim EventObj As New EventLogObject(Me, Log.Log, Log.Source)
                    Me.EventLogObjects.Add(Log.Log, EventObj)
                End If
            Next
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
        End Try
    End Sub
 
    Public Sub RefreshEventLog(ByVal EventLogName As String)
        Try
            Dim aEventLogs As EventLog() = EventLog.GetEventLogs(Me.MachineName)
            Me.EventLogs.Clear()
            For Each Log As EventLog In aEventLogs
                If Not Me.EventLogs.ContainsKey(Log.Log) Then
                    Me.EventLogs.Add(Log.Log, Log)
                End If
                If LCase(Log.Log) = LCase(EventLogName) Then
                    If Not Me.EventLogObjects.ContainsKey(Log.Log) Then
                        Dim EventObj As New EventLogObject(Me, Log.Log, Log.Source)
                        Me.EventLogObjects.Add(Log.Log, EventObj)
                    Else
                        Me.EventLogObjects.Remove(EventLogName)
                        Dim EventObj As New EventLogObject(Me, Log.Log, Log.Source)
                        Me.EventLogObjects.Add(Log.Log, EventObj)
                    End If
                End If
            Next
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
        End Try
 
    End Sub
 
    Public Function ClearEventLog(ByVal nameEventLog As String) As Boolean
        Dim evLog As EventLog
        Try
            If Me.EventLogs.ContainsKey(nameEventLog) Then
                evLog = Me.EventLogs(nameEventLog)
                evLog.Clear()
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
    End Function
 
    Public Function RemoveEventLog(ByVal NameEventLog As String) As Boolean
        Dim EvLog As EventLog
        Try
            If Me.EventLogs.ContainsKey(NameEventLog) Then
                EvLog = Me.EventLogs(NameEventLog)
                EventLog.Delete(EvLog.Log, Me.MachineName)
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
    End Function
 
    Public Function RemoveEventSource(ByVal SourceName As String) As Boolean
        Try
            If EventLog.SourceExists(SourceName) Then
                EventLog.DeleteEventSource(SourceName, Me.MachineName)
            End If
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
    End Function
 
    Public Function AddEventSource(ByVal SourceName As String, ByVal EventLogName As String) As Boolean
        Try
            If Not EventLog.SourceExists(SourceName) Then
                Dim eventCreateData As New EventSourceCreationData(SourceName, EventLogName)
                eventCreateData.MachineName = Me.MachineName
                EventLog.CreateEventSource(eventCreateData)
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
    End Function
 
    Public Function AddEventLog(ByVal NameSource As String, ByVal nameDisplay As String) As Boolean
 
        Try
            If Not EventLog.SourceExists(NameSource, Me.MachineName) Then
                Dim eventCreateData As New EventSourceCreationData(NameSource, nameDisplay)
                eventCreateData.MachineName = Me.MachineName
                EventLog.CreateEventSource(eventCreateData)
            End If
            Dim eventlogke As New EventLog
            With eventlogke
                .Log = nameDisplay
                .Source = NameSource
                .ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 1)
            End With
 
 
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
 
    End Function
 
    Public Function WriteEventLogEntry(ByVal nameEventLog As String, ByVal source As String, ByVal Message As String, ByVal TypeEvent As EventLogEntryType) As Boolean
        Dim EvLog As EventLog
        Try
            If Me.EventLogs.ContainsKey(nameEventLog) Then
                EvLog = Me.EventLogs(nameEventLog)
                If Not EventLog.SourceExists(source) Then
                    EventLog.CreateEventSource(source, nameEventLog)
                End If
 
                With EvLog
                    .MachineName = Me.MachineName
                    .Log = nameEventLog
                    .Source = source
                    .WriteEntry(Message, TypeEvent)
                End With
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            MsgBox(GetErrorInfo(ex))
            Return False
        End Try
    End Function
 
 
    Public Class EventLogObject
        Private f As New Form
        Private p As New ProgressBar
        Private l As New Label
        Private LocationW As Integer
        Private LocationH As Integer
        Private canceled As Boolean = False
 
        Public Sub New(ByVal parent As EventLogManager, ByVal Name As String, ByVal source As String)
            Me.Parent = parent
            Me.Name = Name
            Me.Source = source
        End Sub
 
        Public Sub New(ByVal parent As EventLogManager, ByVal Name As String, ByVal source As String, ByVal NeedEntries As Boolean)
            Me.Parent = parent
            Me.Name = Name
            Me.Source = source
            If NeedEntries Then               
                GetEntries()
            End If
        End Sub
 
        Public Sub New(ByVal parent As EventLogManager, ByVal Name As String, ByVal source As String, ByVal NeedEntriesWaitForm As Boolean, ByVal mainform As Form)
            Me.Parent = parent
            Me.Name = Name
            Me.Source = source
            If NeedEntriesWaitForm Then
                If Not mainform Is Nothing Then
                    GetEntriesWaitForm(mainform)
                Else
                    MsgBox("Provide the parent form!")
                End If
            End If
        End Sub
 
 
        Public Sub GetEntries()
            Dim i As Int64 = 1
            Dim eventL As EventLog
            Dim eventEntryObj As EventLogEntriesObject
            eventL = Parent.EventLogs(Me.Name)
            For Each entr As EventLogEntry In eventL.Entries
                eventEntryObj = New EventLogEntriesObject(Me)
                With eventEntryObj
                    .Category = entr.Category
                    .DateEvent = CStr(entr.TimeWritten)
                    .EventID = entr.InstanceId
                    .Computer = entr.MachineName
                    .Message = entr.Message
                    .Source = entr.Source
                    .Type = [Enum].GetName(GetType(EventLogEntryType), entr.EntryType)
                    .Index = i 'entr.Index
                End With
                i = i + 1
                Me.EventLogEntries.Add(eventEntryObj)
            Next
 
        End Sub
 
        Public Sub GetEntriesWaitForm(ByVal mainform As Form)
            f.Size = New Point(250, 100)
            f.Text = "Processing Eventlog Entries"
            LocationW = mainform.Location.X + mainform.Width / 2 '(System.Windows.Forms.Application.OpenForms(mainform.Name).Location.X) + (System.Windows.Forms.Application.OpenForms(mainform.Name).Width / 2)
            LocationH = mainform.Location.Y + mainform.Height / 2 '(System.Windows.Forms.Application.OpenForms(mainform.Name).Location.Y) + (System.Windows.Forms.Application.OpenForms(mainform.Name).Height / 2)
            f.FormBorderStyle = FormBorderStyle.FixedDialog
            p.Style = ProgressBarStyle.Blocks
            p.Step = 1
            p.Size = New Point(230, 20)
            p.Location = New Point(10, 20)
            p.Minimum = 0
            f.Controls.Add(p)
            l.Text = "Eventlog Entries : 0/100"
            l.Location = New Point(8, 43)
            l.AutoSize = True
            f.Controls.Add(l)
            AddHandler f.Shown, AddressOf ProcessEntries
            AddHandler f.FormClosing, AddressOf CloseWaitForm
            f.ShowDialog()
        End Sub
        Private Sub CloseWaitForm()
 
            canceled = True
            f.Dispose()
            System.Windows.Forms.Application.OpenForms(0).BringToFront()
        End Sub
 
        Private Sub ProcessEntries()
            f.Left = LocationW
            f.Top = LocationH
            Dim i As Int64 = 1
            Dim eventL As EventLog
            Dim eventEntryObj As EventLogEntriesObject
            eventL = Parent.EventLogs(Me.Name)
            p.Maximum = eventL.Entries.Count
            l.Text = "Eventlog Entries : 0/" + CStr(eventL.Entries.Count)
            For Each entr As EventLogEntry In eventL.Entries
                If canceled Then Exit Sub
                eventEntryObj = New EventLogEntriesObject(Me)
                With eventEntryObj
                    .Category = entr.Category
                    .DateEvent = CStr(entr.TimeWritten)
                    .EventID = entr.InstanceId
                    .Computer = entr.MachineName
                    .Message = entr.Message
                    .Source = entr.Source
                    .Type = [Enum].GetName(GetType(EventLogEntryType), entr.EntryType)
                    .Index = i 'entr.Index
                End With
                i = i + 1
                Me.EventLogEntries.Add(eventEntryObj)
                p.PerformStep()
                l.Text = "Eventlog Entries : " + CStr(i) + "/" + CStr(eventL.Entries.Count)
                Application.DoEvents()
 
            Next
            f.Dispose()
        End Sub
 
        Private strName As String
        Public Property Name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property
 
 
        Private strSource As String
        Public Property Source() As String
            Get
                Return strSource
            End Get
            Set(ByVal value As String)
                strSource = value
            End Set
        End Property
 
        Private objParent As EventLogManager
        Public Property Parent() As EventLogManager
            Get
                Return objParent
            End Get
            Set(ByVal value As EventLogManager)
                objParent = value
            End Set
        End Property
 
 
        Private listEventLogEntries As New List(Of EventLogEntriesObject)
        Public Property EventLogEntries() As List(Of EventLogEntriesObject)
            Get
                Return listEventLogentries
            End Get
            Set(ByVal value As List(Of EventLogEntriesObject))
                listEventLogentries = value
            End Set
        End Property
 
 
        Public Class EventLogEntriesObject
 
            Public Sub New(ByVal parent As EventLogObject)
                Me.Parent = parent
            End Sub
 
 
            Private objParent As EventLogObject
            Public Property Parent() As EventLogObject
                Get
                    Return objParent
                End Get
                Set(ByVal value As EventLogObject)
                    objParent = value
                End Set
            End Property
 
 
            Private strDate As String
            Public Property DateEvent() As String
                Get
                    Return strDate
                End Get
                Set(ByVal value As String)
                    strDate = value
                End Set
            End Property
 
 
            Private strType As String
            Public Property Type() As String
                Get
                    Return strType
                End Get
                Set(ByVal value As String)
                    strType = value
                End Set
            End Property
 
 
            Private strSource As String
            Public Property Source() As String
                Get
                    Return strSource
                End Get
                Set(ByVal value As String)
                    strSource = value
                End Set
            End Property
 
 
            Private strEventID As String
            Public Property EventID() As String
                Get
                    Return strEventID
                End Get
                Set(ByVal value As String)
                    strEventID = value
                End Set
            End Property
 
 
            Private strComputer As String
            Public Property Computer() As String
                Get
                    Return strComputer
                End Get
                Set(ByVal value As String)
                    strComputer = value
                End Set
            End Property
 
 
            Private strCategory As String
            Public Property Category() As String
                Get
                    Return strCategory
                End Get
                Set(ByVal value As String)
                    strCategory = value
                End Set
            End Property
 
 
            Private intIndex As Int64
            Public Property Index() As Int64
                Get
                    Return intIndex
                End Get
                Set(ByVal value As Int64)
                    intIndex = value
                End Set
            End Property
 
 
            Private strMessage As String
            Public Property Message() As String
                Get
                    Return strMessage
                End Get
                Set(ByVal value As String)
                    strMessage = value
                End Set
            End Property
 
        End Class
 
    End Class
 
End Class

1 comment:

  1. Added some initializers to the class:

    New() without getting the eventlog entries
    New() with getting the eventlog entries
    New() with progress form processing entries

    Made GetEntries() a Public Sub
    Added Public Sub GetEntriesWaitForm()

    ReplyDelete