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. 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 15.3 lists the important members of the EventLog class, which belongs to the System.Diagnostics namespace. Table 15.3. Important Members of the EventLog Class Member | Type | Description | CreateEventSource() | Method | Opens an event source to write event information | Delete() | Method | Removes a log resource | DeleteEventSource() | Method | Removes an application's event source from the event log | Entries | Property | Gets the contents of the event log | 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 | 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. The CreateEventSource() method enables you to register the application with an event log; if the event log does not already exist, this method creates it for you. The WriteEntry() method of the EventLog object enables you to write messages to the event log specified by the event source. 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 before writing the entry to the event log. 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. Perform the following steps to learn how to record messages to the Application event log from an ASP.NET application: -
Add a new Visual C# .NET Web application project to the existing solution, and name it Example15_2 . -
Place two Label controls (name one of them lblMessage ), one TextBox control (named txtMessage , with its TextMode property set to MultiLine ), one Button control (named btnWrite ), and five RadioButton controls (named rbInformation , rbWarning , rbError , rbSuccessAudit , and rbFailureAudit ) on the Web form. Set the GroupName property for each of the RadioButton controls to EventType . Arrange the controls as shown in Figure 15.2. Figure 15.2. The design of an application that enables you to write messages into a custom event log. -
Switch to Code view and add the following using directive: using System.Diagnostics; -
Double-click the btnWrite button and add the following code to its Click event handler: private void btnWrite_Click(object sender, System.EventArgs e) { EventLogEntryType eletEntryType; // Set the eletEntryType member if (rbWarning.Checked) eletEntryType = EventLogEntryType.Warning; else if (rbInformation.Checked) eletEntryType = EventLogEntryType.Information; else if (rbSuccessAudit.Checked) eletEntryType = EventLogEntryType.SuccessAudit; else if (rbFailureAudit.Checked) eletEntryType = EventLogEntryType.FailureAudit; else eletEntryType = EventLogEntryType.Error; string strSourceName = "Example15_2"; try { //If no event source exists, create an event source if(!EventLog.SourceExists(strSourceName)) { EventLog.CreateEventSource(strSourceName, "Application"); } // Write an entry into event log EventLog.WriteEntry(strSourceName, txtMessage.Text, eletEntryType); lblMessage.Text = "Entry written to the log successfully"; } catch(Exception ex) { lblMessage.Text = ex.Message; } } -
Set Example15_2 as the startup project of the solution and run the project. Enter a message in the message text box and select the type of message from the radio button options. Click the Write to Log button to write to the event log. -
To view the logged messages, navigate to Server Explorer, expand the Servers node, select the node corresponding to your computer, and expand it. Right-click the Events node and select Launch Event Viewer from its shortcut menu. This action has the same effect as launching the Event Viewer from the Administrative Tools section of the Windows Control Panel. When executing the program, if you get an error message stating Requested registry access is not allowed , it means that the default user for the ASP.NET worker process ( ASPNET ) does not have the correct user rights to create an event source. You can perform the following steps to resolve this problem: -
Select Start, Run. Type regedit to open the Windows Registry Editor. -
Navigate through the Registry hierarchy to the following Registry subkey : HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application -
Right-click the Application subkey; then select New, Key. -
Type Example15_2 for the key name. Of course, when you deploy your application, you do not want the user to manually edit the Windows Registry. In that case, you can create the required Registry keys automatically during the setup process by using the System.Diagnostics.EventLogInstaller class. | The Security log is read-only for all users. | |