EventLog


The EventLog component enables an application to manipulate event logs. It provides methods to create logs, write and read log messages, and clear logs.

The following code demonstrates some of the EventLog component’s features. Before a program can write into an event log, it must register with the log as an event source. When this program’s form loads, it uses the EventLog class’s shared SourceExists method to see if the StatusSource event source is defined. If the source is not defined, the program uses the CreateEventSource method to define it. The program sets the logStatus EventLog component’s Source property to StatusSource and uses the component’s WriteEntry method to place an entry in the log. It then calls the ShowLog subroutine described shortly to display the log entries. The form’s Closing event handler writes a message into the log indicating that the program is closing.

When the user clicks the program’s Add To Log button, the code writes the text in the txtMessage text box into the log, clears the text box, and calls ShowLog to display the log entries.

When the user clicks the Clear Log button, the program calls the EventLog component’s Clear method to remove all of the entries from the log.

Finally, subroutine ShowLog clears the lstLog list box. It then loops through the EventLogEntry objects contained in the component’s Entries collection, adding each object’s message to the list box.

  ' Connect to the log. Private Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     ' See if the log exists and create it if it      If Not EventLog.SourceExists("StatusSource") Then         EventLog.CreateEventSource("StatusSource", "StatusLog")         Debug.WriteLine("Creating event source")     End If     ' Set the EventLog component's source.     logStatus.Source = "StatusSource"     ' Write a Starting message to the log.     logStatus.WriteEntry(Now & "> " & "Starting")     ShowLog() End Sub ' Write a Closing message to the log. Private Sub Form1_FormClosing(ByVal sender As Object, _  ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing     logStatus.WriteEntry(Now & "> " & "Closing") End Sub ' Add an entry to the log. Private Sub btnAddToLog_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnAddToLog.Click     logStatus.WriteEntry(Now & "> " & txtMessage.Text)     txtMessage.Text = ""     txtMessage.Select(0, 0)     ShowLog() End Sub ' Enable or disable btnAddToLog. Private Sub txtMessage_TextChanged(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles txtMessage.TextChanged     btnAddToLog.Enabled = (txtMessage.Text.Length > 0) End Sub ' Clear the log. Private Sub btnClearLog_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnClearLog.Click     logStatus.Clear()     ShowLog() End Sub ' List the items in the log. Private Sub ShowLog()     lstLog.Items.Clear()     For Each log_entry As EventLogEntry In logStatus.Entries         lstLog.Items.Add(log_entry.Message)     Next log_entry     lstLog.SelectedIndex = lstLog.Items.Count - 1 End Sub 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net