Writing Events to an Existing Log


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 20.  Writing to the Event Log

Writing Events to an Existing Log

If you examine the event log, you will notice that it's not crammed full of procedural-level statements indicating everything your application is or isn't doing. The event log does contain significant information about applications that were unable to start, services that failed, perhaps unavailable database connections, and other application, system, or security information that is essential to the proper initialization of your system or an application. This doesn't mean, however, that you cannot use the event log for application line items at a more granular level. What it does suggest is that you shouldn't use the existing Application, System, or Security logs for this purpose.

You will find it beneficial to ship your applications with some logging. When a subsystem is unable to start, perhaps because an Internet connection or database is unavailable, you may want to log that information to the Application log to help users troubleshoot. Logging subsystem failures will point troubleshooters to specific problems and help guide them to a specific resolution.

Generally the hardware or operating system writes entries to the System log. The Security log is read-only. This leaves the Application log or a custom log as the recipient for your application's critical event information. To log application information to the Application log, you need to set a log source, call WriteEntry, and close the log when you are finished.

You only need to create the event source once. You can check to see if the event source exists when your application starts. If it doesn't exist, create it. After the event source exists, you can write entries to the indicated log using shared or instance versions of WriteEntry. When your application shuts down, you can call the Close instance method. If you have used the shared form of EventLog.WriteEntry, you don't have to explicitly call Close.

 Private Sub Form1_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles MyBase.Load   If (Not EventLog.SourceExists("Application")) Then     EventLog.CreateEventSource("EventLogDemo", "Application")   End If End Sub 

The fragment demonstrates initializing the source and log in the OnLoad event of a form. The main form is a good candidate for creating the source. Adding the WriteEntry statement to the Except block of an exception handler is a reasonable place to write event log entries. The simplest form is the shared WriteEntry( string ) method. There are five overloaded shared versions of WriteEntry. (Check out the help file for details on each version.) The following statement adds an entry to the "EventLogDemo" source, indicating that the source was created. Several forms of WriteEntry allow you to indicate the event log entry type.

 EventLog.WriteEntry("EventLogDemo", "Created Source", _  EventLogEntryType.Information) 

EventLogEntryType is an enumeration that determines the icon that is displayed with the log entry (see Figure 20.3). Valid entry types are Error, FailureAudit, Information, SuccessAudit, and Warning.

Figure 20.3. Windows 2000 Event Viewer.


The EventLog.Close method requires an instance and takes no parameters.

Writing to the event log is a time-consuming process. Microsoft recommends that you only write event information in error-handling blocks to reduce the overhead of writing to the event log.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222

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