Working with Performance Counters
Instrument and debug a Windows service, a serviced component, a .NET Remoting object, and an XML Web service.
Performance counters are Windows' way of collecting performance data from running processes. Microsoft Windows itself provides several hundred performance counters, each of which monitors a particular system parameter. In addition, various .NET server products, such as SQL Server, Exchange Server, and various .NET Framework applications, also publish their own custom performance counters.
Windows organizes performance counters into categories. Each category defines a specific set of performance counters. For example, there are categories such as Memory, Processor, and PhysicalDisk, and the Memory category has various counters such as Available Bytes, Cache Bytes, and Committed Bytes.
Some categories are further divided into instances. For example, the Process category is divided into several instances, each representing a running process on the computer. A new instance is added to the category whenever a new process is started, and the instance is removed when a process is killed. Each instance can have performance counters (such as I/O Read Bytes/sec) that specify the memory activity of a process. Usually all the instances in a category have the same list of performance counters. Of course each of the performance counters has unique performance data associated with it.
You can view existing performance counters in a navigable format in Visual Studio .NET Server Explorer, as shown in Figure.
38. Visual Studio .NET Server Explorer displays all the existing performance counters on a machine.
Note from Figure that .NET CLR Data, .NET CLR Memory, and other nodes represent performance counter categories. Such nodes as # Total Committed Bytes and # Total Reserved Bytes are the performance counters in the .NET CLR Memory category. The _Global_, aspnet_wp, devenv, and others are the instances of the # Total Committed Bytes performance counter.
The PerformanceCounter class allows you to read performance samples for processes that are running on a machine. By using this class, an application can even publish its own performance counter to inform the world about its performance level.
Figure lists some important members of the PerformanceCounter class.
Figure Important Members of the PerformanceCounter Class
Specifies the performance counter category name.
Closes the performance counter and frees all the resources.
Specifies the performance counter description.
Specifies the performance counter name.
Specifies the performance counter type.
Decrements the performance counter value by one.
Increments the performance counter value by one.
Increments or decrements the performance counter value by a specified amount.
Specifies the instance name.
Specifies the computer name.
Returns an object of type CounterSample that has properties such as RawValue, BaseValue, TimeStamp, and SystemFrequency. These properties provide you with detailed information on the performance data.
Retrieves the current calculated value (a float type) for a performance counter.
Retrieves the raw, or uncalculated, value for a performance counter.
Indicates whether the PerformanceCounter object is in read-only mode.
Deletes an instance from the PerformanceCounter object.
The .NET Framework allows applications to create their own custom performance counters and publish their performance data. This performance data can then be monitored via the Performance Monitoring tool (perfmon.exe).
Visual Studio .NET makes it easy for you to create new performance categories and counters: It provides the Performance Counter Builder Wizard, which is available via Server Explorer. In Step-by-Step 9.12, you create a Windows application that publishes its performance and allows you to manually increase or decrease its performance.
9.12 Publishing Performance Data
Add a Visual C# Windows application project to the solution. Name the project StepByStep9_12.
Rename the Form1.cs file StepByStep9_12.cs in the project. Switch to the code view of the form and modify all references to Form1 so that they refer to StepByStep9_12 instead.
Place five Label controls (of which one is named lblCurrentLevel and two represent lines), one TextBox control (txtLevel), and three Button controls (btnInc, btnDec, and btnSet) on the form. Arrange the controls as shown in Figure.
39. You can publish custom performance data for an application.
Open Server Explorer and select from the Servers node the server in which you want to create a performance counter. Right-click the Performance Counters node and select the Create New Category option. The Performance Counter Builder dialog box appears. Enter the values in the dialog box, as shown in Figure, and then click OK.
40. The Performance Counter Builder dialog box enables you to create a new performance counter category and specify one or more counters to be placed within it.
Creating a Custom Performance Counter
When you create a new performance counter, you must specify a completely new category for your performance counter. It is not possible to add a new performance counter to an existing category; however, you can add several performance counters to a new custom category.
It is also possible to create performance counter categories programmatically by using the PerformanceCounterCategory.Create() method.
Select the StepByStep9_12 performance counter from the Performance Counters node in Server Explorer. Drag the performance counter that appears under the StepByStep9_12 node on the form. Set the Name of the counter to pc9_12 and the ReadOnly property to false.
Switch to the code view. Add the following using directive:
Attach Click event handlers to the button controls and add the following code in the event handlers:
private void btnSet_Click(
object sender, System.EventArgs e)
// Set the performance counter value
pc9_12.RawValue = Int32.Parse(txtLevel.Text);
private void btnInc_Click(object sender,
private void btnDec_Click(object sender,
Attach Load and Closed event handlers to the form and add the following code in the event handlers:
private void StepByStep9_12_Load(
object sender, System.EventArgs e)
private void StepByStep9_12_Closed(
object sender, System.EventArgs e)
pc9_12.RawValue = 0;
Set the project StepByStep9_12 as the startup project.
Run the project. Enter a number in the text box and click the Set button. The label immediately reflects the current value of the performance counter. Click the Increment and Decrement buttons and notice the value changing in the label.
The System event log provides a central repository for various issues that an application may encounter while it is executing. Using an event log to record such messages not only makes the job of system administrator easy, but it allows other applications to take appropriate action when an entry is written to a log.
Multiple event sources can write into an event log. However, an event source can be used to write into only one event log.
Performance counters are organized in categories, and each category defines a specific set of performance counters.
The process of reading a performance counter value is called sampling the performance counter. The process of updating a performance counter value is called publishing a performance counter.