The event log is the standard destination for applications to record problems and issues. You can easily monitor the behavior of an application by analyzing its messages in the event log. There are several ways to read the event log messages: Administrators like to read them through the Event Viewer tool, developers can directly access the contents of the event log from Visual Studio .NET, and applications can read the event log messages through the classes provided by the Framework Class Library. Programmatic access to the event log enables you to automate some of the administrative tasks associated with an application. By default, three event logs are available: Application, Security, and System. Other applications (including .NET applications) or operating system components, such as Active Directory, might add other event logs. Table 10.6 lists the important members of the EventLog class. Table 10.6. Important Members of the EventLog ClassMember | Type | Description |
---|
CreateEventSource() | Method | Registers the application with an event log. If the event log does not already exist, this method creates it for you. | Delete() | Method | Removes a log resource. | DeleteEventSource() | Method | Removes an application's event source from the event log. | EnableRaisingEvents | Property | Indicates whether the EventLog object should receive notifications for the EntryWritten event. | Entries | Property | Gets the contents of the event log. | EntryWritten | Event | Occurs when an entry is written to an event log on the local computer. | Exists() | Method | Determines whether the specified log exists. | GetEventLogs() | Method | Creates an array of the event logs. | Log | Property | Specifies the name of the log to read from or write to. | LogDisplayName | Property | Represents a friendly name for the event log. | LogNameFromSourceName() | Method | Gets the name of the log to which the specified source is registered. | MachineName | Property | Specifies the name of the computer on which to read or write events. | Source | Property | Specifies the source to register and use when writing to an event log. | SourceExists() | Method | Finds whether a given event source exists. | WriteEntry() | Method | Writes an entry in the event log. | Each application interested in interacting with an event log must register an event source with the log. After an event source is registered, its information is stored in the system Registry and is available across application restarts. If the event source specified by the Source property of an EventLog object does not exist, the first call to the WriteEntry() method creates the event source. You can write different types of messages (information, error, warning, success audit, and failure audit) to an event log. These types are specified by the values in the EventLogEntryType enumeration, as shown here: string strSourceName = "Chapter10"; //If no event source exist, create an event source if(!EventLog.SourceExists(strSourceName)) EventLog.CreateEventSource(strSourceName, "Application"); // Write an entry into event log EventLog.WriteEntry(strSourceName, "Message", EventLogEntryType.Information); | The Security log is read-only for all users. |
|