Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C# 4.0

Performance Counters Enumerator

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
11 Feb 2010CPOL2 min read 34.7K   1.1K   29   6
Enumerate all performance counters registered on a machine and save the result in an XML file

Introduction

Some articles have already been written about Windows Performance Counters. Nevertheless, I still experience that their potential is underestimated by administrators and often almost unknown to many software developers.

Performance_Counters_Enumerator.png

One of the main reasons for this is the fact that they are just too many Performance counters! On a plain end-user machine, they are several hundreds of them. The amount of performance counters increases vertically on a Server.

Goal

The goal of this article is to present a way to enumerate all performance counters registered on a local machine and to give the possibility to save the result of the enumeration in an XML file. Once saved in an XML file, one can transform the result using XSLT and present it in a nice «look and feel» report.

xml_editpro.jpg

The motivation is to make a kind of snapshot of all performance counters available on a machine. This will help developers and administrators to choose the ideal performance counters based on their description when needed. Often when dealing with diagnose and the Windows built-in «Performance Monitor», I am overwhelmed with the huge amount of these counters and the lack of having a comprehensive list of the performance counters without having to click several hundred times in the MMC dialog.

Implementation

Using the available .NET Diagnostic classes, it is just a matter of a few minutes to reach this goal.

Since this project only enumerates the category of performance counters and all performance counters belonging to these categories, I only need two of the available classes that deal with performance counters.

PerformanceCounterCategory and PerformanceCounter both belong to the System.Diagnostics Namespace which, as stated in the MSDN documentation, mainly provides classes that allow you to interact with:

  • system processes
  • event logs
  • and performance counters

Having said that, this tool consists of four steps:

  1. Retrieve the name of the local machine where the tool runs:
    C#
    // Retrieve the machine's name. 
    txtMachine.Text = Environment.MachineName; 
  2. Retrieve the Categories of Performance Counters available on the machine:
    C#
    // Get the Categories of Performance Counters
    performanceCountersMgr.GetPerformanceCountersCategories(txtMachine.Text.ToString());
    PerformanceCounterCategory[] categories = performanceCountersMgr.Categories;
    for (int i = 0; i < categories.Length; i++)
    {
        // Show these at the UI
        UpdateViewCategories(categories[i]);
    }
  3. For any selected Category, retrieve the Counters available:
    C#
    // Collect Counters for the selected Category
    ListView.SelectedListViewItemCollection sel = listViewCategories.SelectedItems;
    if(sel != null && sel.Count > 0)
    {
        // Show Description of the selected Category
        PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
        textBoxCategoryDescription.Text = category[sel[0].Index].CategoryHelp;
        // Acquire the Counters of the selected Category
        PerformanceCounter[] counters = 
    	performanceCountersMgr.GetPerformanceCounters(category[sel[0].Index]);
        if (counters != null)
        {
            for (int i = 0; i < counters.Length; i++)
            {
                PerformanceCounter counter = counters[i];
                UpdateViewCounters(counter);
            }
            listViewCounters.Items[0].Selected = true;
        }
    }
  4. Save the result at the desired location as an XML File:
    C#
    XmlTextWriter xml = new XmlTextWriter(filename, Encoding.UTF8);
    xml.WriteStartDocument(true);
    xml.WriteStartElement("Categories");
    C#
    PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
    C#
    for (int i = 0; i < category.Length; i++)
    {
        xml.WriteStartElement("Categorie");
        xml.WriteStartAttribute("Name");
        xml.WriteString(category[i].CategoryName);
    C#
        if (checkBoxCategoryDescription.Checked)
        {
            xml.WriteStartAttribute("Description");
            xml.WriteString(category[i].CategoryHelp);
        }
            xml.WriteEndAttribute();
            xml.WriteEndElement(); 
    }
    C#
    // End of Categories Element
    xml.WriteEndElement(); 
    xml.Close();

Conclusion

This article shows how to enumerate the performance counters on a local machine using just a few of the available .NET Diagnostic classes and how to serialize the enumeration in an XML file using the XmlTextWriter class. The enumeration of these on a remote machine is left as an exercise for the reader.

History

  • 11.02.2010 - Enumeration of the categories of performance counters and their associated counters

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer winitor
Germany Germany
Marc Ochsenmeier is the author of pestudio (www.winitor.com) and worked as developer with the focus on Windows Security. He now works as a Malware Analyst

pestudio is on twitter at: https://twitter.com/ochsenmeier

Comments and Discussions

 
GeneralThis is great! Pin
CZabransky10-Mar-11 6:52
CZabransky10-Mar-11 6:52 
I'm working on a performance monitoring client and this information is very useful!

My concern is that I need to be able to iterate through each instance contained within a counter. Do you know of a way to do this?
GeneralRe: This is great! Pin
marc ochsenmeier16-May-11 6:14
marc ochsenmeier16-May-11 6:14 
GeneralMy vote of 5 Pin
Dirk Held4-Nov-10 3:19
Dirk Held4-Nov-10 3:19 
GeneralRe: My vote of 5 Pin
marc ochsenmeier16-May-11 6:12
marc ochsenmeier16-May-11 6:12 
GeneralMinor bug and question Pin
klanglieferant15-Feb-10 20:03
klanglieferant15-Feb-10 20:03 
GeneralRe: Minor bug and question Pin
marc ochsenmeier21-Feb-10 22:43
marc ochsenmeier21-Feb-10 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.