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:
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 CountersYou 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 CountersThe 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:
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:
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 PageInstead 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 :
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.
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. |