How to Write an Entry to the Application Event Logs in Visual Basic 2008

In this tutorial, I will teach you how to Write an Entry to the Application Event Logs by using Visual Basic 2008. Logs are very important because it will track every event that are happening in your application. So, let’s begin: 1. Open Visual Basic 2008. 2. Create a new Windows Application. 3. Make the Form just like this. First Form Event Logs Now, go to the code view. Create a variable that will set the entry type of the event log. And then, set it to default type which is “information”.
  1. Private entry_type As System.Diagnostics.EventLogEntryType = EventLogEntryType.Information
After that, you have to create a method for the entry type that you're going to use. Combine the three Radio Button that handles the click events.
  1. Private Sub rdo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  2. Handles rdoWarning.Click, rdoInfo.Click, rdoError.Click
  3.  
  4. 'THIS PROCEDURE HANDLES THE CLICK EVENT OF ALL THE RADIO BUTTONS.
  5. 'YOU WILL KNOW WHICH RADIO BUTTON WAS CLICKED BECAUSE
  6. ' IT WILL PASS IN AS THE "SENDER" ARGUMENT.
  7. Dim rdo As RadioButton = CType(sender, RadioButton)
  8. 'SET THE PROPERTY NAME OF THE RADIO BUTTONS
  9. Select Case rdo.Name
  10. Case "rdoWarning" 'NAME OF THE RADIO BUTTON
  11. entry_type = EventLogEntryType.Warning
  12. Case "rdoInfo" 'NAME OF THE RADIO BUTTON
  13. entry_type = EventLogEntryType.Information
  14. Case "rdoError" 'NAME OF THE RADIO BUTTON
  15. entry_type = EventLogEntryType.Error
  16. End Select
  17.  
  18. End Sub
Lastly, do this following code for writing an entry for your event logs.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Try
  3. If IsNumeric(txtId.Text) Then
  4. 'WRITE THE NAME OF THE LOG ON THE FIRST PARAMETERS.
  5. 'FOR THE SECOND PARAMETER, IS THE NAME OF THE LOCAL MACHINE.
  6. 'AND FOR THE THIRD PARAMETER, IS THE SOURCE OF THE EVENT.
  7. 'THIS IS SET COMMONLY EQUAL TO THE NAME OF THE APPLICATION.
  8. Dim eventLog As New EventLog("Application", My.Computer.Name, "Write Event Logs")
  9.  
  10. 'SET THE FIRST PARAMETER FOR THE MESSAGE THAT YOU PUT IN THE TEXT BOX.
  11. 'FOR THE SECOND PARAMETER, IS THE ENTRY TYPE YOU WANT TO SET WHETHER
  12. ' IT IS A WARNING , AN IMFORMATION OR AN ERROR.
  13. 'AND THE THIRD PARAMETER, IS FOR THE ID OF THE EVENT.
  14. ' YOU CAN USE THIS TO LOOK FOR FURTHER INFORMATION IN THE HELP FILE
  15. eventLog.WriteEntry(txtname.Text, entry_type, CInt(txtId.Text))
  16.  
  17. eventLog.Close()
  18.  
  19. MsgBox("Entry written to the event log.", MsgBoxStyle.OkOnly, Me.Text)
  20. Else
  21. 'THIS WILL PERFORM IF THE EVENT ID WAS NOT NUMERIC.
  22. MsgBox("Put the numeric value into EventID text box.", MsgBoxStyle.OkOnly, _
  23. Me.Text & " Error")
  24. End If
  25. Catch secEx As System.Security.SecurityException
  26. MsgBox("Security error writing to the event log.", _
  27. MsgBoxStyle.OkOnly, Me.Text & " Error")
  28. Catch ex As Exception
  29. MsgBox("Error accessing logs on the local machine.", MsgBoxStyle.OkOnly, Me.Text & " Error")
  30. End Try
  31. End Sub

Add new comment