Displaying Information about the ASP.NET ProcessBy Scott Mitchell
|As a Follow-Up...|
|This article examines how to read information about the ASP.NET worker process. If you are after more general system-level information, be sure to read: Displaying Performance Monitor Information through an ASP.NET Web Page. In that article we look at how to read data from the Windows Performance Monitor (such as free memory, CPU utlization, ASP.NET information, IIS information, etc.) and display it through an ASP.NET Web page!|
In a previous article (How Long has the Web Server Been Up?) we examined a way to display the number of milliseconds the Web server computer had been running. This metric measured the time the actual machine had been running without a reboot, not the time that IIS had been running. Regardless, such a metric is useful if you want to keep an eye and ensure that your Web host is keeping the servers up, or if you are just wanting to create an administration-information page for your company's intranet. In this article, we will look at how to display more performance-related metrics.
With ASP.NET you can configure the "ASP.NET engine" (
aspnet_wp.exe) to automatically restart itself when certain criteria are met. For example, if the Web server has 100 requests queued up, you may wish to have the Web server restarted, assuming that some script or DLL is hanging the Web server. Additionally, you can set requirements on restarting the Web server on the memory usage, the total number of requests, or by the total time the Web server's run. This proactive approach was implemented by the ASP.NET team in order to increase the overall uptime of a Web site running ASP.NET: if the memory suddenly shoots through the roof, the Web server can be restarted, reigning in the memory usage and starting anew.
To set these Web server restart parameters, you can specify the defaults in
Just look under the
<processModel> tab. Also, for more information on this setting,
be sure to check out this article.
The information regarding the life cycle of the ASP.NET engine can be examined through the use
ProcessModelInfo class. This class contains two static methods:
returns an instance of the
ProcessInfo class, which contains information about the
ASP.NET process, such as when it was started, its peak memory usage, why it was last shutdown, its
process ID, how long its currently been running, and its current status. The
method returns an array of
ProcessInfo classes, representing information about previous
n ASP.NET processes.
Displaying Information About the Currently Running ASP.NET Process
GetCurrentProcessInfo()method you can find out information about the current process. For example, you may wish to create an Administrator's page that would display the process ID, start time, running time, and peak memory used of the ASP.NET process. The code for this is fairly straightforward, and can be seen below:
Notice that we begin our
Page_Load event handler by creating a variable of type
ProcessInfo and set it to the
ProcessInfo instance returned by the
GetCurrentProcessInfo() static method of the
Next, we assign some of the properties of the
ProcessInfo class to label Web controls
down in the HTML section.
One caveat: I have noted the
PeakMemoryUsed property to be a bit... funny. The
say that the output is in megabytes, but clearly the ASP.NET process is not taking up over 35,000 MB of
memory. Rather, I think the docs are in error and it should read the number of kilobytes used.
Displaying the Reasons for Previous Shutdowns
Displaying information about the current ASP.NET process is cool enough, but an administrator might find it even more useful to be able to view the last 10 ASP.NET processes, showing when they came up, how long they lived, and why they went down.
To accomplish this we need to use the
GetHistory(n) method of the
class to get the information on the last n ASP.NET process instances. Recall that the
GetHistory(n) returns an array of
ProcessInfo instances; to display this
information we need only get the array of
ProcessInfo instances, loop through each of them
For Each ... Next loop, and output their properties similarly to how we outputted
the information about the current ASP.NET process. While this way works, it's very reminicent of
classic ASP. Why not use more modern methods - databinding! We can databind the array of
ProcessInfo class instances to a DataGrid Web control:
Notice that our
Page_Load event handler is painfully simple - we just need to call the
ProcessModelInfo.GetHistory(10) to get an array of
representing the last 10 ASP.NET processes. We specify this array as the DataGrid's
property and then bind the datasource to the DataGrid by calling the
Using ASP.NET, you'll find that your Web server has better uptime since ASP.NET can proactively restart itself when it gets into a jam (too much memory used, a deadlock, too many requests in the queue, etc.) Determining information about the current and previous ASP.NET processes is simple using the
ProcessModelInfoclass's two static methods -
GetHistory(n). In a future article we'll look at how to determine statistics about the actual Web server machine, such as free memory, CPU utilization, etc. Until then, Happy Programming!