To read the article online, visit http://www.4GuysFromRolla.com/articles/021502-1.aspx

Displaying Information about the ASP.NET Process

By 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!

Introduction


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.

Using the ProcessModelInfo Class


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 machine.config. 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 of the ProcessModelInfo class. This class contains two static methods: GetCurrentProcessInfo() and GetHistory(n). The GetCurrentProcessInfo() 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 GetHistory(n) method returns an array of ProcessInfo classes, representing information about previous n ASP.NET processes.

Displaying Information About the Currently Running ASP.NET Process


Using the 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:

<script runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
      Dim objCurrentInfo as ProcessInfo = ProcessModelInfo.GetCurrentProcessInfo()
  
      lblProcID.Text = objCurrentInfo.ProcessID  
      lblStartTime.Text = objCurrentInfo.StartTime  
      lblRunningTime.Text = objCurrentInfo.Age.Days & " days, " & _
               objCurrentInfo.Age.Hours & ":" & objCurrentInfo.Age.Minutes  
      lblPeakMem.Text = objCurrentInfo.PeakMemoryUsed
  End Sub
</script>

<html>
<head>
</head>
<body>
  <h1 align="center">Web Server Statistics</h1>
  <table cellspacing="1" align="center" border="1">
    <tbody>
      <tr>
        <td align="left">ASP.NET Process ID: 
        </td>
        <td align="middle">
          <asp:label id="lblProcID" runat="server"></asp:label>
        </td>
      </tr>
      <tr>
        <td align="left">ASP.NET was Started at: 
        </td>
        <td align="middle">
          <asp:label id="lblStartTime" runat="server"></asp:label>
        </td>
      </tr>
      <tr>
        <td align="left">ASP.NET has been running for: 
        </td>
        <td align="middle">
          <asp:label id="lblRunningTime" runat="server"></asp:label>
        </td>
      </tr>
      <tr>
        <td align="left">Peak Memory Used: 
        </td>
        <td align="middle">
          <asp:label id="lblPeakMem" runat="server"></asp:label>
          KB 
        </td>
      </tr>
    </tbody>
  </table>
</body>
</html>

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 ProcessModelInfo class. 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 docs 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 ProcessModelInfo 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 in a 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:

<script runat="server">
    Sub Page_Load(sender as Object, e as EventArgs)
        dgHistory.DataSource = ProcessModelInfo.GetHistory(10)
        dgHistory.DataBind()
    End Sub
</script>

<asp:datagrid id="dgHistory" runat="server" HorizontalAlign="center"
    AutoGenerateColumns="False" CellPadding="4">
      
  <HeaderStyle Font-Bold="True" BackColor="#dddddd" />
  <Columns>
    <asp:boundcolumn HeaderText="Process ID" DataField="ProcessID" />
    <asp:boundcolumn HeaderText="Start Time" DataField="StartTime" />
    <asp:boundcolumn HeaderText="Peak Memory Used (in KB)"
      DataField="PeakMemoryUsed" />
    <asp:boundcolumn HeaderText="Shutdown Reason" DataField="ShutdownReason" />
  </Columns>
</asp:datagrid>

Notice that our Page_Load event handler is painfully simple - we just need to call the ProcessModelInfo.GetHistory(10) to get an array of ProcessInfo instances representing the last 10 ASP.NET processes. We specify this array as the DataGrid's DataSource property and then bind the datasource to the DataGrid by calling the DataBind() method. Neat!

Conclusion


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 ProcessModelInfo class's two static methods - GetCurrentProcessInfo() and 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!

  • By Scott Mitchell

  • Article Information
    Article Title: ASP.NET.Displaying Information about the ASP.NET Process
    Article Author: Scott Mitchell
    Published Date: February 15, 2002
    Article URL: http://www.4GuysFromRolla.com/articles/021502-1.aspx


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers