How to Read Entry Event Logs in Visual Basic 2008
Submitted by janobe on Thursday, May 22, 2014 - 13:14.
In my last tutorial I taught you how to write an entry to the application event log. This time, I will teach you how to read your written entry event logs by using Visual Basic 2008. This will assure you that the log that you wrote was written. Then, it will be shown in the RichTextBox.
Let's Begin:
Open Visual Basic 2008, create a new Windows Application and do the Form just like this.
Go to the solution explorer and hit the view code.
In the code view, declare a string variable for storing the name of the logs you want to view.
After that, go to the design views, double lick the Button to fire the
This will be the order of the codes that you have made.
Now, press F5 and click the Button to fire the in the method.
- 'CREATE A STRING VARIABLE FOR
- ' STORING THE NAME OF THE LOGS THAT YOU WANT TO VIEW.
- Private log_Type As String = "Application"
click
event handler. And do this following code in the method.
- Try
- 'FOR THE MAXIMUM LOGS THAT WILL DISPLAY
- Const DisplayEntry As Integer = 20
- 'EVENT LOG CONSTRUCTOR HAS PASSED A STRING VARIABLE FOR THE LOG NAME.
- Dim event_log As New EventLog(log_Type, System.Environment.MachineName, _
- "Event Log Sample")
- 'ADD THIS MESSAGE TO THE RICHTEXTBOX AND PUT TWO SPACES BELOW OF IT.
- RichTextBox1.Text = "Event log entries (maximum of 20), newest to oldest." & vbCrLf & vbCrLf
- Dim lastLogShow As Integer = event_log.Entries.Count - DisplayEntry
- If lastLogShow < 0 Then
- lastLogShow = 0
- End If
- 'DISPLAY THE LAST 20 RECORDS IN THE LOG.
- For index As Integer = event_log.Entries.Count - 1 To lastLogShow Step -1
- Dim Current_Entry As EventLogEntry = event_log.Entries(index)
- RichTextBox1.Text &= "Event ID : " & Current_Entry.InstanceId & vbCrLf
- RichTextBox1.Text &= "Entry Type : " & _
- Current_Entry.EntryType.ToString() & vbCrLf
- RichTextBox1.Text &= "Message : " & _
- Current_Entry.Message & vbCrLf & vbCrLf
- Next
- Catch sec_Ex As System.Security.SecurityException
- 'CATCHING SECURITY ERRORS.
- Catch ex As Exception
- 'CATCH ANY ERRORS.
- End Try
- Public Class Form1
- 'CREATE A STRING VARIABLE FOR
- ' STORING THE NAME OF THE LOGS THAT YOU WANT TO VIEW.
- Private log_Type As String = "Application"
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Try
- 'FOR THE MAXIMUM LOGS THAT WILL DISPLAY
- Const DisplayEntry As Integer = 20
- 'EVENT LOG CONSTRUCTOR HAS PASSED A STRING VARIABLE FOR THE LOG NAME.
- Dim event_log As New EventLog(log_Type, System.Environment.MachineName, _
- "Event Log Sample")
- 'ADD THIS MESSAGE TO THE RICHTEXTBOX AND PUT TWO SPACES BELOW OF IT.
- RichTextBox1.Text = "Event log entries (maximum of 20), newest to oldest." & vbCrLf & vbCrLf
- Dim lastLogShow As Integer = event_log.Entries.Count - DisplayEntry
- If lastLogShow < 0 Then
- lastLogShow = 0
- End If
- 'DISPLAY THE LAST 20 RECORDS IN THE LOG.
- For index As Integer = event_log.Entries.Count - 1 To lastLogShow Step -1
- Dim Current_Entry As EventLogEntry = event_log.Entries(index)
- RichTextBox1.Text &= "Event ID : " & Current_Entry.InstanceId & vbCrLf
- RichTextBox1.Text &= "Entry Type : " & _
- Current_Entry.EntryType.ToString() & vbCrLf
- RichTextBox1.Text &= "Message : " & _
- Current_Entry.Message & vbCrLf & vbCrLf
- Next
- Catch sec_Ex As System.Security.SecurityException
- 'CATCHING SECURITY ERRORS.
- Catch ex As Exception
- 'CATCH ANY ERRORS.
- End Try
- End Sub
- End Class
Add new comment
- 92 views