Working with Server Performance Counters

As we examined in "Accessing the Windows Event Log," the .NET Framework contains a System.Diagnostics namespace that houses classes which can be used for diagnostic information. One such class is the EventLog class, which we just looked at in the previous section; another useful diagnostics class is the PerformanceCounter class. This class gives the developer direct read access to the existing Windows Performance counters and provides the capability to create new performance counters!

Because this functionality is encapsulated in the .NET Framework, these techniques can be used through an ASP.NET page. That means you can create an ASP.NET page that reports information on the Web server's various resources! Talk about a remote administrator's dream!

Performance counters, which are represented by the PerformanceCounter class, are defined by at least two required properties. The first two required properties are CategoryName and CounterName; these represent the category and counter one sees when selecting a performance monitor through the Administrative Performance tool (see Figure 2.31). For example, to view the percentage of the processor's power being used, you'd be interested in the "% Processor Time" counter of the "Processor" category.

Common ASP.NET Code Techniques

Chapter 2

Figure 2.31

When choosing a performance counter through the administrative interface, a category and counter are selected.

Figure 2.31

When choosing a performance counter through the administrative interface, a category and counter are selected.

For certain performance counters, a third property, InstanceName, is required. Specifically, the InstanceName is needed for counters that monitor a metric with multiple instances, such as Web server information. Because a single Web server can run many different Web sites, we must specify the Web site if we want to view specific Web server-related information (such as total number of requests).

When creating an instance of the PerformanceCounter, you can specify these two (or three) properties in the constructor. For example, to create an instance of the PerformanceCounter class that could be used to report the total number of threads, the following code could be used:

Dim objPerf as New PerformanceCounter("System", "Threads")

The previous snippet creates an instance of the PerformanceCounter class named objPerf. This instance represents the performance counter "Threads" found in the "System" category.

To read the performance counter's value, simply use the NextValue() method. NextValue() returns a single representing the value of the next performance counter's sample. Therefore, we could amend our previous code to display the total number of threads with a simple call to NextValue():

Dim objPerf as New PerformanceCounter("System", "Threads") Response.Write("There are " & objPerf.NextValue() & " threads.")

Listing 2.30 illustrates a very simple ASP.NET page that could possibly serve as a handy tool for an off-site Web site administrator. The code for Listing 2.30, when viewed through a browser, will display the total available memory for the Web server, the number of processes currently running on the Web server, and the total number of ASP.NET page compilations for the particular Web application in which the ASP.NET page resides. The output is shown in Figure 2.32.

Listing 2.30 Gather System Information about the Web Server via an ASP.NET Page

1: <%@ Import Namespace="System.Diagnostics" %>

2: <script language="VB" runat="server">

3: Sub Page_Load(source as Object, e as EventArgs)

4: Dim objMemPerf as New PerformanceCounter("Memory", "Available Bytes") 5: lblFreeMemory.Text = Single.Format(objMemPerf.NextValue(), "#,###") & 1 bytes" 6:

Dim objProcPerf as New PerformanceCounter("System", "Processes") lblProcesses.Text = objProcPerf.NextValue().ToString()

10 11 12

20 21 22

0 0

Post a comment

  • Receive news updates via email from this site