The Windows Event Log

I l @ ve RuBoard

An event is anything of significance or importance in an application or operating system that requires users to be notified via an entry added to a log file. The Windows Event Log is a service on Windows NT, Windows 2000, and Windows XP that runs automatically when the computer boots. The Event Log service is responsible for recording these significant application, security, and system events to a log file. The Event Viewer allows you to examine the events logged to a given event log. By default, Windows contains System, Security, and Application Event logs, although custom event logs can be created by an application. Event logs can help you identify and diagnose system or application problems.

Writing to the Windows event log is a must when you deal with a service because it is one of the best and easiest ways to debug and trace the execution of a service. An event log can be created from within an application in three ways. We'll briefly discuss each option and show a fairly trivial example of creating a custom log on the computer, setting the log source, and writing to the log. Creating an event log is extremely simple using the .NET Framework EventLog class. These three approaches all yield the same result ”an instance of an EventLog class:

  • Add an EventLog component from the Components tab of the Toolbox by dragging it to the designer.

  • In Server Explorer, find the specific event log on the machine and drag it onto your designer.

  • Create the EventLog instance in code, as in the following example:

     PublicModuleLogger Privatem_logAsEventLog PublicSubLogEvent(ByValmessageAsString,_ ByValeventTypeAsEventLogEntryType) 'ChecktoseeifwehaveavalidEventLogobject ' If(m_logIsNothing)Then IfNotEventLog.SourceExists("TimeService")Then EventLog.CreateEventSource("TimeService", "Utilities") EndIf 'CreateanEventLoginstanceandassignitssource. ' m_log=NewEventLog("Utilities") m_log.Source= "TimeService" EndIf m_log.WriteEntry(message,eventType) EndSub EndModule 

The LogEvent sub shown in the preceding example takes the textual message for the event as an input parameter. If the EventLog instance has not been created, the code checks to see whether the " TimeService" source exists on the computer. If the source does not exist, a new custom event log called Utilities is created. The code then creates an instance of the EventLog in the private m_log variable and sets the Source property of this log instance to the TimeService event source.

Caution

If you don't call CreateEventSource in your code, the first time you call WriteEntry , it will call CreateEventSource for you to create the event source. If the log property has not previously been set, then the event will be written to the Application log by default.


Finally, the code writes the event to the custom log as the type specified by the eventType parameter. Many overloads are available for the WriteEntry method, including parameters for setting the EventLogEntryType , registered event source, and application-defined event identifiers and categories, as well as parameters for appending binary data.

The following snapshot of the Event Viewer shows the Utilities custom event log after the service has inserted an entry.

The EventLog class has several other properties, methods, and events for managing a Windows event log. This class includes methods for retrieving an array of event logs on a specific computer, deleting event logs and EventLog sources, reading from an event log, and an event which signals when an entry has been written to a log.

Figure 7-5. The Utilities custom event log.

graphics/f07pn05.jpg

I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

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