Writing To Windows Event Log in Visual Basic.NET

Written By: Kevin Jordan

- 09 Mar 2006 -
















Description: This simple application uses the EventLog class to create a form for adding custom messages to the system and application log.

  1. Creating the Form
  2. The WriteToEvent Function
  3. Programming the Events

The ClearForm Subroutine

The ClearForm subroutine is used when the application loads, after submitting each log entry, and of course when the Clear button is pressed.

Private Sub ClearForm()
        cmbLevel.SelectedIndex = 0
        cmbType.SelectedIndex = 0
        txtSource.Text = ""
        txtDescription.Text = ""
End Sub ' ClearForm 

The first thing it does is sets the two combo boxes to the first index, Application for cmbType and Information for cmbLevel. Without this, the page option would be blank when the program loads and potentially empty values could be sent to the WriteToEvent function. The txtSource.Text and txtDescription.Text is pretty self explanatory. It just sets the two text boxes to an empty value.

On Load and btnClear_Click

These two events behave in the same way. They call the ClearForm subroutine to ensure that all values in the form are set to the default state.

Private Sub frmEventWrite_Load (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
        Call ClearForm()
End Sub ' frmEventWrite_Load
 
Private Sub btnClear_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClear.Click
        Call ClearForm()
End Sub ' btnClear_Click 


btnSubmit_Click Event

The btnSubmit_Click does quite a bit more than the last two events. There are a couple parameters checks just to be safe, and then the WriteToEvent function is called based on the supplied values.

Private Sub btnSubmit_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim strDescription, strLevel, strType, strSource

Start out by declaring a few variables to copy the values from the input fields. Instead of passing the contents of the input fields directly to the function, use an intermediate variable. Otherwise, during the parameter checks if the user left a field blank, they would see the field momentarily change while the log was being processed. It may not seem important, but sometime the little things can make a big difference.

If txtDescription.Text = "" Then
        MessageBox.Show("An error description is required.")
        Return
Else
        strDescription = txtDescription.Text
End If
 
If cmbLevel.Text = "" Then
        strLevel = "Information"
Else
        strLevel = cmbLevel.Text
End If
 
If cmbType.Text = "" Then
        strType = "Application"
Else
        strType = cmbType.Text
End If
 
If txtSource.Text = "" Then
        strSource = "VB.NET Application"
Else
        strSource = txtSource.Text
End If

Without specifying an error description, this application isn’t really serving a purpose; therefore make it a required field. If it’s empty it will pop up with a mean note and stop executing the subroutine. The other fields aren’t quite as important, so anything that is empty is set to a default value before the function is called.

If strLevel = "Error" Then
        WriteToEventLog(strDescription, strSource, EventLogEntryType.Error, strType)
ElseIf strLevel = "Warning" Then
        WriteToEventLog(strDescription, strSource, EventLogEntryType.Warning, strType)
Else
        WriteToEventLog(strDescription, strSource, EventLogEntryType.Information, strType)
End If

This condition statement checks to see what error level is being used and then executes the WriteToEventLog function using the correct EventLogEntryType value. Because the function returns a Boolean value, it is possible to do some addition error checking.

MessageBox.Show("Your event has been recorded.")
Call ClearForm()

Here’s the end. If everything goes according to plan then the user will get a handy dandy little message and the form will be cleared so they can do it all over again.

<< Previous