Add Performance Counters to Your Apps

Monitor individual apps with custom performance counters.

UI Tips




Add Performance Counters to Your Apps

Monitor individual apps with custom performance counters.


By Brad McCabe


ASP.NET provides numerous new performance counters that let you monitor the state and health of the overall condition of your Web servers; but what about custom counters for your application?


ASP.NET lets you monitor individual applications easily with custom performance counters. Using the performance counter APIs, you can publish unique and application-specific data. This ability is useful for real-time monitoring of your applications, such as orders received, orders received per second, and so on.


To create your own performance counter, import the System.Diagnostics namespace into your application:


Imports Namespace="System.Diagnostics"


Once you add the import statement to the top of your code-behind page, you can create the PerformanceCounter object. In this example, the counter is created and incremented during the page load event, but you can use this logic in other events and locations as needed:


Sub Page_Load(Sender as Object, E as EventArgs)


    Dim MyCounter As PerformanceCounter

    MyCounter = New PerformanceCounter("MyStore", "Total Orders", "app1")




End Sub


The counter object has two main methods for incrementing the count. The Increment method increments the counter by one, whereas the IncrementBy method increments the counter by the amount specified in the parameter.


The performance counters not only record data for you to view in PerfMon, it also lets you read data from the counters and react to it. For example, you might want to send an e-mail to the administrator when the number of orders hits 1,000.


If you want to read from the counter, call its NextValue method to take a reading of the state of the counter:


    Dim MyCounter As PerformanceCounter

    MyCounter = New PerformanceCounter("MyStore", "Total Orders", "app1")


   If MyCounter.NextValue = 1000 Then

      MySendMailFunction("We got 1,000 orders!!")

   End If


The power of the PerformanceCounter object applies in the amount of monitoring and management that you enable in your applications with a relative amount of effort. Now you can monitor the unique state of your application in real time and react to various states in your code.


Got a UI question, tip, or idea for a topic you'd like me to cover? I'd love to hear it. E-mail me at [email protected].


Brad McCabe is the technical evangelist for Infragistics. Brad also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist within Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at [email protected].





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.