Logging Events


The Windows 2000 operating system includes a centralized set of system event logs. You can use these centralized logs to record events from your ASP.NET applications to a standard location.

You add new entries or retrieve existing entries from your server's event logs by using the EventLog class. The EventLog class includes a WriteEntry() method that you can use to write a new event to the event log. When you write a new entry to the event log, you write the entry to a particular log using a particular event source.

The event log can be the Application, System, or Security event log. Or, the event log can be a custom log. When writing entries from an ASP.NET page, you will normally want to use the Application event log since there are fewer security issues when writing to this log.

The event source is the source of the log entry. You can name your event source anything that you please . For example, when writing to an event log from an ASP.NET application, you might want to create an event source named My ASP.NET App.

Event sources must be unique for a event log. When you create a new event source, you are adding an entry to the registry. Since writing to the registry requires special security permissions, you need to create your event source outside of your ASP.NET application. There is a special command-line utility included on the CD that accompanies this book named CreateEventSource.exe that will allow you to create a new event source. You'll need to run this utility to create a new event source before you can write entries to the event log (the source for the tool is also include on the CD).

For example, if you use the CreateEventSource.exe utility to create a new event source named My ASP.NET App, then you can use the page in Listing 18.23, which contains a form that enables you to insert a new entry into the Application event log.

Listing 18.23 AddEventLog.aspx
 <%@ Import Namespace="System.Diagnostics" %> <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   Dim objLog As EventLog   objLog = New EventLog   objLog.Source = "My ASP.NET App"   objLog.WriteEntry( txtLogEntry.Text, EventLogEntryType.Information ) End Sub </Script> <html> <head><title>AddEventLog.aspx</title></head> <body> <form Runat="Server"> <asp:TextBox   ID="txtLogEntry"   Runat="Server" /> <asp:Button   Text="Add Event!"   OnClick="Button_Click"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

The Button_Click subroutine in Listing 18.23 adds a new event to the Application event log. First, an instance of the EventLog class is created. The Source property is associated with the name of the event source. Finally, the WriteEntry method is called to add the event to the event log.

The first parameter passed to the WriteEntry method represents the message to add to the event log. You can write any message that you please. The second parameter represents the type of the message. The EventLogEntryType enumeration has the following five values:

  • Error

  • FailureAudit

  • Information

  • SuccessAudit

  • Warning

You can use the EventLog class with the Application_Error subroutine in the Global.asax file to automatically log all errors in your ASP.NET application to a server event log. The Global.asax file in Listing 18.24 illustrates how to log all unhandled application errors to the Application event log. (You can find this page in the LogError directory on the CD that accompanies this book.)

Listing 18.24 LogError/Global.asax
 <%@ Import Namespace="System.Diagnostics" %> <Script Runat="Server"> Sub Application_Error   Dim strErrorText As String   Dim objLog As EventLog   strErrorText = "ERROR IN " & Request.Path   strErrorText &= vbNewline & vbNewline & "ERROR:"   strErrorText &= Server.GetLastError.InnerException.Message   strErrorText &= vbNewline & vbNewline & "STACK TRACE:"   strErrorText &= Server.GetLastError.InnerException.StackTrace   objLog = New EventLog   objLog.Source = "My ASP.NET App"   objLog.WriteEntry( strErrorText, EventLogEntryType.Error ) End Sub </Script> 

The C# version of this code can be found on the CD-ROM.

If you add the Global.asax file in Listing 18.24 to the root directory of your application, any error generated by any page in your application is recorded to the event log. The CD that accompanies this book includes a page named ErrorPage.aspx in the LogError folder. If you request this page, you'll receive an error and a new entry will be added to the Application event log.

You can view entries in the CustomLog event log by opening Event Viewer on your server. Alternatively, you can use the ASP.NET page in Listing 18.25 to display all the entries in the Application event log.

Listing 18.25 DisplayEventLog.aspx
 <%@ Import Namespace="System.Diagnostics" %> <Script Runat="Server"> Sub Page_Load   Dim objLog As EventLog   objLog = New EventLog   objLog.Log = "Application"   dgrdLog.DataSource = objLog.Entries   dgrdLog.DataBind() End Sub </Script> <html> <head><title>DisplayEventLog.aspx</title></head> <body> <asp:DataGrid   ID="dgrdLog"   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 18.25, all the entries from the Application event log are retrieved with the Entries property of the EventLog class. The log entries are displayed in a DataGrid control.

NOTE

You can use the page in Listing 18.25 to view the Application, System, or Security log by changing the Log property to point to the appropriate log file. For example, to view the System event log, you would set the Log property like this:

 
 myLog.Log = "System" 

Using Performance Counters

You can use performance counters to monitor the performance of your ASP.NET applications. The ASP.NET framework includes several standard performance counters that you can use.

Using ASP.NET Performance Counters

The standard ASP.NET performance counters can be divided into two groups: system performance counters and application performance counters. The system performance counters contain information about every executing ASP.NET application, whereas the application performance counters contain information about a particular ASP.NET application.

The system performance counters include the following counters:

  • Application Restarts ” The total number of ASP.NET application restarts since Internet Information Server was last started. Specifically, this counter counts each Application_End event.

  • Application Running ” The total number of executing ASP.NET applications.

  • Requests Disconnected ” The total number of requests that were disconnected due to a communication failure.

  • Requests Queued ” The total number of requests waiting in the queue.

  • Request Wait Time ” The amount of time, in milliseconds , that the last request waited for processing in the queue.

  • Worker Process Restarts ” The total number of times that the ASP.NET process has been restarted.

  • Worker Process Running ” The total number of executing ASP.NET processes.

You can use the application counters to monitor the performance of a particular ASP.NET application instance, or an aggregate of all application instances, by using the __Total__ instance. The following is a partial list of the application counters:

  • Authentication Anonymous Requests ” The total number of anonymous requests

  • Authentication Anonymous Requests/Sec ” The number of anonymous requests made per second

  • Debugging Requests ” The number of requests that occurred while debugging was enabled

  • Errors During Compilation ” The number of errors that occurred during compilation

  • Errors During Execution ” The total number of errors that occurred during the execution of an HTTP request

  • Errors During Preprocessing ” The number of errors that occurred during parsing

  • Errors Unhandled During Execution ” The total number of unhandled errors that occurred during the execution of HTTP requests

  • Errors Unhandled During Execution/Sec ” The number of unhandled exceptions per second that occur during the execution of HTTP requests

  • Total Errors ” The total number of errors that occur during the execution of HTTP requests, including any parsing, compilation, or runtime errors

  • Total Errors/Sec ” The number of errors per second that occur during the execution of HTTP requests, including any parsing, compilation, or runtime errors

  • Request Bytes in Total ” The total size of all requests in bytes

  • Request Bytes out Total ” The total size of responses sent to a client in bytes (not including standard HTTP response headers)

  • Requests Executing ” The number of requests currently executing

  • Requests Failed Total ” The total number of failed requests, including requests that timed out, requests that were not authorized (status code 401), or requests not found (404 or 414)

  • Requests Not Found ” The number of requests that failed because resources were not found (status code 404, 414)

  • Requests Not Authorized ” The number of requests that failed due to no authorization (status code 401)

  • Requests Succeeded ” The number of requests that executed successfully and returned with status code 200

  • Requests Timed Out ” The number of requests that timed out

  • Requests Total ” The total number of requests since the service was started

  • Requests/Sec ” The number of requests executed per second

  • Sessions Active ” The current number of sessions currently active

  • Sessions Abandoned ” The number of sessions that have been explicitly abandoned

  • Sessions Timed Out ” The number of sessions that timed out

  • Sessions Total ” The total number of sessions

You can view any of these ASP.NET system or application performance counters by launching Windows Performance Monitor. In Windows 2000, go to Start, Programs, Administrative Tools, Performance. To view a particular counter, click the Add button and select the counter that you want to view from the dialog box.

NOTE

You also can launch Performance Monitor by typing perfmon at a command prompt.


By monitoring the Requests/Sec performance counter, for example, you can determine how well your Web application scales . By monitoring the Total Errors performance counter, you can detect whether your site is experiencing problems.

Retrieving Performance Counters in an ASP.NET Page

Instead of monitoring performance counters through Windows Performance Monitor, you can retrieve counter values directly through an ASP.NET page. You can use the classes from the System.Diagnostics namespace to work with performance counters within your application.

To retrieve an object that represents a particular performance counter, you need to supply the performance counter category name, performance counter name, and performance counter instance name. For example, the following statements create a counter that represents the Total Requests performance counter:

 
 Dim objCounter As PerformanceCounter objCounter = New PerformanceCounter( "ASP.NET Applications", "Requests Total", "__Total__" ) 

The first parameter, ASP.NET Applications , represents the performance counter category name. The second parameter, Requests Total , represents a particular performance counter. Finally, the last parameter, __Total__ , represents the performance counter instance name (the instance name __Total__ is preceded and followed by two underscore characters ).

After you create an instance of a performance counter, you can read its values by using one of the following properties or methods :

  • RawValue ” Returns the raw value of the performance counter at the time the counter is read

  • NextValue ” Returns the calculated value of the counter

  • NextSample ” Returns a sample of the value of the counter that you can use when calculating statistics for the counter with the Calculate method of the CounterSample class

The page in Listing 18.26, for example, uses the NextValue method to return the value of a particular counter. The page enables you to view the value of any ASP.NET Application counter for any application instance (see Figure 18.11).

Listing 18.26 ReadCounters.aspx
 <%@ Import Namespace="System.Diagnostics" %> <Script Runat="Server"> Sub Page_Load   If Not IsPostBack Then     Dim objCategory As PerformanceCounterCategory     objCategory = New PerformanceCounterCategory( "ASP.NET Applications" )     lstInstances.Datasource = objCategory.GetInstanceNames()     DataBind()   End If End Sub Sub SelectInstance( s As Object, e As EventArgs )   Dim objCategory As PerformanceCounterCategory   objCategory = New PerformanceCounterCategory( "ASP.NET Applications" )   lstCounters.Visible = True   lstCounters.Datasource = objCategory.GetCounters( lstInstances.SelectedItem.Text )   lstCounters.DataTextField = "CounterName"   DataBind() End Sub Sub SelectCounter( s As Object, e As EventArgs )   Dim objCounter As PerformanceCounter   objCounter = New PerformanceCounter( "ASP.NET Applications", _     lstCounters.SelectedItem.Text, lstInstances.SelectedItem.Text )   lblValue.Text = objCounter.NextValue() End Sub </Script> <html> <head><title>ReadCounters.aspx</title></head> <body> <h2>ASP.NET Application Performance Counters</h2> <form Runat="Server"> <b>Instances</b> <br> <asp:ListBox   ID="lstInstances"   AutoPostback="True"   OnSelectedIndexChanged="selectInstance"   Runat="Server" /> <p> <b>Counters</b> <br> <asp:ListBox   ID="lstCounters"   Visible="False"   AutoPostback="True"   OnSelectedIndexChanged="selectCounter"   Runat="Server" /> <p> <b>Counter Value:</b> <asp:Label   ID="lblValue"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Figure 18.11. Displaying performance counters.

graphics/18fig11.jpg

In the Page_Load subroutine in Listing 18.26, a list of instance names is returned from the PerformanceCounterCategory class and assigned to a ListBox control. If you pick a performance counter instance from the ListBox control, the SelectInstance subroutine executes.

The SelectInstance subroutine retrieves a list of performance counters for a particular instance with the GetCounters method of the PerformanceCounterCategory class. The list of counters is assigned to a ListBox control. If you pick a counter in the ListBox control, the SelectCounter subroutine is executed.

The SelectCounter subroutine uses the NextValue method to display the value of the selected performance counter. The value is displayed in a Label control.

You should be aware of two issues when reading performance counters in an ASP.NET page. First, the page in Listing 18.26 uses the NextValue method to retrieve the value of a counter. Several performance counters, such as Requests/Sec, require you to read the counter value more than once to retrieve a value other than 0. To retrieve a useful value from this type of counter, you need to call the NextValue method multiple times or use the NextSample method.

You also should be aware that, in the case of performance counters, to be is to be perceived. The values of all the performance counters are reset to 0 when the last component that references the counter is disposed. This means, unless you have an application that is continuously reading the performance counter, you won't get very accurate information from the performance counter.

You can get around this limitation in two ways: Keep Windows Performance Monitor running or start the Performance Counter Windows Service. The Performance Counter Service was installed on your server when you installed the .NET framework. To start the service, go to Start, Programs, Administrative Tools, Services. Running either Performance Monitor or the Performance Counter Service prevents your counters from being reset.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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