Hi.
Im working on getting certain Event Log Entries from the Event Log.
What Id like to achieve is:
A user can select which Event Log Entry Type he wants to view. E.g: Error, Warning, Information etc
He should also be able to choose from which category he wants the logs. E.g: System, Application etc
After he has selected, only the Last 10 entries of that (selected) type should be saved to a text file.
Help is really appreciated. If you want I can upload the whole Project.
I have really no idea how to achieve this, but I did get the following code from a website which gets the event log entries and outputs to a rich textbox, but It doesnt achieve what I want to.
Im working on getting certain Event Log Entries from the Event Log.
What Id like to achieve is:
A user can select which Event Log Entry Type he wants to view. E.g: Error, Warning, Information etc
He should also be able to choose from which category he wants the logs. E.g: System, Application etc
After he has selected, only the Last 10 entries of that (selected) type should be saved to a text file.
Help is really appreciated. If you want I can upload the whole Project.
I have really no idea how to achieve this, but I did get the following code from a website which gets the event log entries and outputs to a rich textbox, but It doesnt achieve what I want to.
Code:
Public Class ReadForm
' Stores the name of the log that the user wants to view.
Private logType As String = ""
Private Sub cmdViewLogEntries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewLogEntries.Click
Try
Const EntriesToDisplay As Integer = 10
' In this case the EventLog constructor is passed a string variable for the log name.
' This is because the user of the application can choose which log they wish to view
' from the listbox on the form.
Dim ev As New EventLog(logType, System.Environment.MachineName, "Event Log Sample")
rchEventLogOutput.Text = "Event log entries (maximum of 10), newest to oldest." & vbCrLf & vbCrLf
Dim lastLogToShow As Integer = ev.Entries.Count - EntriesToDisplay
If lastLogToShow < 0 Then
lastLogToShow = 0
End If
' Display the last 10 records in the chosen log.
For index As Integer = ev.Entries.Count - 1 To lastLogToShow Step -1
Dim CurrentEntry As EventLogEntry = ev.Entries(index)
'rchEventLogOutput.Text &= "Event ID : " & CurrentEntry.InstanceId & vbCrLf
rchEventLogOutput.Text &= "Event ID : " & CurrentEntry.EntryType.Error & vbCrLf
rchEventLogOutput.Text &= "Entry Type : " & EventLogEntryType.Error.ToString() & vbCrLf
rchEventLogOutput.Text &= "Message : " & CurrentEntry.Message & vbCrLf & vbCrLf
Next
Catch secEx As System.Security.SecurityException
MsgBox("Security exception in reading the event log.", MsgBoxStyle.OKOnly, Me.Text & " Error")
Catch ex As Exception
MsgBox("Error accessing logs on the local machine.", MsgBoxStyle.OKOnly, Me.Text & " Error")
End Try
End Sub
Private Sub lstEntryType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEntryType.SelectedIndexChanged
' Store the log that the user selected in the ListBox
logType = CType(lstEntryType.Items(lstEntryType.SelectedIndex()), String)
End Sub
Private Sub ReadForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each currentLog As EventLog In EventLog.GetEventLogs()
lstEntryType.Items.Add(currentLog.LogDisplayName)
Next
lstEntryType.SelectedIndex = 0
Catch secEx As System.Security.SecurityException
MsgBox("Security exception in reading the event log.", MsgBoxStyle.OKOnly, Me.Text & " Error")
Catch ex As Exception
MsgBox("Error accessing logs on the local machine.", MsgBoxStyle.OKOnly, Me.Text & " Error")
End Try
End Sub
End Class