When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML
Information:
Advertise
Feedback
Author an Article

ASP ASP.NET ASP FAQs Message Board Feedback
 
Print this Page!
Published: Wednesday, April 10, 2002

Displaying Performance Monitor Information through an ASP.NET Web Page, Part 2

By Scott Mitchell


  • Read Part 1

  • In Part 1 we looked at the security issues involved with reading performance counter data through an ASP.NET Web page. We looked at how to overcome this with two approaches: having all ASP.NET requests use a system-level account (bad); and setting up Windows authentication and impersonation to allow a Web visitor with a Windows account on the Web server's domain to log in and perform tasks suitable to his Windows user's permissions. In this part we'll examine how to read performance monitor data from an ASP.NET Web page!

    - continued -

    Reading Performance Monitor Data


    The .NET Framework contains a System.Diagnostics namespace that contains classes to perform a plethora of diagnostic analysis, such as accessing the event log and performance counters. (For more information on some of the really cool things you can do with ASP.NET, including reading from the Event Log, check out the sample chapter of ASP.NET: Tips, Tutorials, and Code.) The class we're most interested in here is the PerformanceCounter class, which, as its name implies, allows for access to performance counters.

    To read from a performance counter we must first know the category name, counter name, and instance name of the counter we wish to read. The easiest way, I've found, for finding out these values is to launch the Windows Performance Monitor. When you opt to add a new counter to the Performance Monitor, you are presented with the dialog box shown to the right. Note that for each counter there are three vital pieces of information: the Performance Object (on the right Processor is selected); the counter (on the right % Processor Time is selected); and instances (on the right _Total is selected). When creating an instance of the PerformanceCounter class you need to pass in this information into the class's constructor. (Note that the instances property is optional. In some performance counters, there is no notion of "instance." With the Processor category, however, there is the notion of different instances, for I may have multiple processors in my box - so I may choose to view statistics from all of them (_Total) or a particular one. From the dialog box on the right you can see that I only have one processor in my computer.)

    The constructor for the PerformanceCounter class has a number of forms. Two of the most useful are:

    PerformanceCounter(categoryName, counterName)
    -- and --
    PerformanceCounter(categoryName, counterName, instanceName)

    Here all input parameters are strings. Note that the instanceName is optional; as aforementioned, there are a number of performance categories where the notion of separate instances doesn't make sense.

    Displaying the Amount of Free Memory


    Now that we've taken a high-level look at how one can create an instance of the PerformanceCounter class, let's look at actually doing something: displaying on an ASP.NET Web page the number of free megabytes of physical memory. To accomplish this, we want to create a PerformanceCounter class instance with category name Memory and counter name Available MBytes (there is no instance setting for the Memory category). To create a PerformanceCounter class instance we use the following code:

    Dim perfFreeMem as New PerformanceCounter("Memory", "Available MBytes")

    At this point we have an object named perfFreeMem that represents the appropriate performance counter. In order to read the current value from the counter we simply call the NextValue() method, which returns the next value of the performance counter. We may wish to output this value by assigning it to the Text property of a label control, as shown below:

    <%@ Import Namespace = System.Diagnostics %>
    <script runat="server">
      sub Page_Load(sender as Object, e as EventArgs)
        Dim perfFreeMem as New PerformanceCounter("Memory", "Available MBytes")
        
        lblFreeMem.Text = perfFreeMem.NextValue()
      end sub
    </script>
    
    Free Memory (in MB): 
    <asp:Label id="lblFreeMem" runat="server" />
    

    Note that in the above code the first thing we do is import the System.Diagnostics namespace. Next, as we examined before, we create an instance of the PerformanceCounter class set to read in the Available MBytes counter from the Memory category. Finally, we set the value returned by NextValue() to the label control lblFreeMem. Pretty easy, eh? (To see another code example of reading from the performance counters, scroll to the end of this article...)

    Conclusion


    One of the neat things about ASP.NET is that it is, literally, part of the .NET Framework. Hence, your ASP.NET Web pages have access to all of the classes available in the .NET Framework. This means that through an ASP.NET Web page you can do all sorts of cool stuff, like image manipulations, writing to the event log, or, as we examined in this article, reading from the performance monitor counters. Of course there were some security issues that we had to concern ourselves with, but once those were out of the way, writing code to use the performance counter was a breeze!

    Happy Programming!

  • By Scott Mitchell


    Another Performance Counter Example...
    <%@ Import Namespace = System.Diagnostics %>
    
    <script runat="server">
    
      sub Page_Load(sender as Object, e as EventArgs)
        Dim perfAppRestarts as New PerformanceCounter("ASP.NET", "Application Restarts")
        Dim perfFreeMem as New PerformanceCounter("Memory", "Available MBytes")
        
        lblAppRestarts.Text = perfAppRestarts.NextValue()
        lblFreeMem.Text = perfFreeMem.NextValue()
      end sub
    
    </script>
    
    <html>
    <head>
    </head>
    <body>
        <form runat="server">
            <p>
                Application Restarts:  
                <asp:Label id="lblAppRestarts" runat="server"></asp:Label>
            </p>
            <p>
                Free Memory (in MB): 
                <asp:Label id="lblFreeMem" runat="server"></asp:Label>
            </p>
        </form>
    </body>
    </html>
    



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article