How to Read Entry Event Logs in Visual Basic 2008

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. FirstForm Read Entry Go to the solution explorer and hit the view code. Code View Read Entry In the code view, declare a string variable for storing the name of the logs you want to view.
  1. 'CREATE A STRING VARIABLE FOR
  2. ' STORING THE NAME OF THE LOGS THAT YOU WANT TO VIEW.
  3. Private log_Type As String = "Application"
After that, go to the design views, double lick the Button to fire the click event handler. And do this following code in the method.
  1. Try
  2. 'FOR THE MAXIMUM LOGS THAT WILL DISPLAY
  3. Const DisplayEntry As Integer = 20
  4.  
  5.  
  6. 'EVENT LOG CONSTRUCTOR HAS PASSED A STRING VARIABLE FOR THE LOG NAME.
  7. Dim event_log As New EventLog(log_Type, System.Environment.MachineName, _
  8. "Event Log Sample")
  9.  
  10. 'ADD THIS MESSAGE TO THE RICHTEXTBOX AND PUT TWO SPACES BELOW OF IT.
  11. RichTextBox1.Text = "Event log entries (maximum of 20), newest to oldest." & vbCrLf & vbCrLf
  12.  
  13.  
  14. Dim lastLogShow As Integer = event_log.Entries.Count - DisplayEntry
  15. If lastLogShow < 0 Then
  16. lastLogShow = 0
  17. End If
  18.  
  19. 'DISPLAY THE LAST 20 RECORDS IN THE LOG.
  20. For index As Integer = event_log.Entries.Count - 1 To lastLogShow Step -1
  21. Dim Current_Entry As EventLogEntry = event_log.Entries(index)
  22. RichTextBox1.Text &= "Event ID : " & Current_Entry.InstanceId & vbCrLf
  23. RichTextBox1.Text &= "Entry Type : " & _
  24. Current_Entry.EntryType.ToString() & vbCrLf
  25. RichTextBox1.Text &= "Message : " & _
  26. Current_Entry.Message & vbCrLf & vbCrLf
  27. Next
  28. Catch sec_Ex As System.Security.SecurityException
  29. 'CATCHING SECURITY ERRORS.
  30. MsgBox("Security Error in reading the event log.", MsgBoxStyle.OkOnly, Me.Text & " Error")
  31. Catch ex As Exception
  32. 'CATCH ANY ERRORS.
  33. MsgBox("Error for accessing logs on the local machine.", MsgBoxStyle.OkOnly, Me.Text & " Error")
  34. End Try
This will be the order of the codes that you have made.
  1. Public Class Form1
  2.  
  3. 'CREATE A STRING VARIABLE FOR
  4. ' STORING THE NAME OF THE LOGS THAT YOU WANT TO VIEW.
  5. Private log_Type As String = "Application"
  6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7. Try
  8. 'FOR THE MAXIMUM LOGS THAT WILL DISPLAY
  9. Const DisplayEntry As Integer = 20
  10.  
  11.  
  12. 'EVENT LOG CONSTRUCTOR HAS PASSED A STRING VARIABLE FOR THE LOG NAME.
  13. Dim event_log As New EventLog(log_Type, System.Environment.MachineName, _
  14. "Event Log Sample")
  15.  
  16. 'ADD THIS MESSAGE TO THE RICHTEXTBOX AND PUT TWO SPACES BELOW OF IT.
  17. RichTextBox1.Text = "Event log entries (maximum of 20), newest to oldest." & vbCrLf & vbCrLf
  18.  
  19.  
  20. Dim lastLogShow As Integer = event_log.Entries.Count - DisplayEntry
  21. If lastLogShow < 0 Then
  22. lastLogShow = 0
  23. End If
  24.  
  25. 'DISPLAY THE LAST 20 RECORDS IN THE LOG.
  26. For index As Integer = event_log.Entries.Count - 1 To lastLogShow Step -1
  27. Dim Current_Entry As EventLogEntry = event_log.Entries(index)
  28. RichTextBox1.Text &= "Event ID : " & Current_Entry.InstanceId & vbCrLf
  29. RichTextBox1.Text &= "Entry Type : " & _
  30. Current_Entry.EntryType.ToString() & vbCrLf
  31. RichTextBox1.Text &= "Message : " & _
  32. Current_Entry.Message & vbCrLf & vbCrLf
  33. Next
  34. Catch sec_Ex As System.Security.SecurityException
  35. 'CATCHING SECURITY ERRORS.
  36. MsgBox("Security Error in reading the event log.", MsgBoxStyle.OkOnly, Me.Text & " Error")
  37. Catch ex As Exception
  38. 'CATCH ANY ERRORS.
  39. MsgBox("Error for accessing logs on the local machine.", MsgBoxStyle.OkOnly, Me.Text & " Error")
  40. End Try
  41. End Sub
  42. End Class
Now, press F5 and click the Button to fire the in the method.

Add new comment