Writing the event log VB.NET

This code shows how to write event log in VB.NET

Private Sub cmdWriteEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnWriteEntry.Click

If IsNumeric(txtEventID.Text) Then

' When writing to an event log you need to pass the machine name where
' the log resides. Here the MachineName Property of the Environment class
' is used to determine the name of the local machine. Assuming you have
' the appropriate permissions it is also easy to write to event logs on
' other machines.

' The first entry is the name of the log you want to write to. The second
' parameter is the machine name. In this case it is the local machine name.
' The third parameter is the source of the event. Commonly this is set equal
' to the name of the application that is writing the event.

Dim ev As New EventLog("Application", System.Environment.MachineName, _
"How-To Use the Event Log")

' The first argument to WriteEntry is the text of the message. The second
' argument is the type of entry you want to create (Warning, Information, etc.)
' The third is the eventID of the event. The user could use this to look up
' further information in a help file.

ev.WriteEntry(txtEntry.Text, entryType, CInt(txtEventID.Text))


MessageBox.Show("Entry written to the event log", _
"frmWrite", _
MessageBoxButtons.OK, _
' The EventID was not numeric
MessageBox.Show("Value entered into EventID textbox must be numeric", _
"Event ID Entry Error", _
MessageBoxButtons.OK, _
End If

Catch secEx As SecurityException
MessageBox.Show("Error writing to the event log: this may be due to a lack of appropriate permissions." & vbCrLf & secEx.Message, _
"Lack of Permissions", _
MessageBoxButtons.OK, _
Catch ex As Exception
MessageBox.Show("Error accessing logs on the local machine." & vbCrLf & ex.Message, _
"Error accessing logs.", _
MessageBoxButtons.OK, _
End Try

End Sub


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: