Performance Counter Programming Architecture
The PerformanceCounter component uses these portions of the Microsoft .NET Framework namespaces:
When you add an instance of the PerformanceCounter component to your application, Visual Studio .NET automatically creates the references and import statements you need to access the namespace and classes. However, if you are creating your PerformanceCounter instances in code, you might need to create a reference to the System.Diagnostics namespace and add a statement to your project importing (in Visual Basic) or using (in C#) the appropriate namespace.
For more information on adding and removing project references, see Adding and Removing References.
Key Performance Counter Classes
The following are the major classes associated with PerformanceCounter components:
- System.Diagnostics.PerformanceCounter — the base class used to create PerformanceCounter components and interact with their values.
- System.Diagnostics.PerformanceCounterCategory — provides access to the categories for which performance counters exist on a server and all of their corresponding properties and methods. You use methods in this class to create new categories and custom counters, to verify that categories and counters exist, and to read the existing counters in a category.
- System.Diagnostics.CounterCreationData — used to create multiple counters for a category; allows you to explicitly choose the counter type for each counter you create.
- System.Diagnostics.InstanceData — used to retrieve multiple instances of a counter and all of their corresponding properties and methods.
Key Programming Elements in the PerformanceCounter and PerformanceCounterCategory Classes
You can use the following methods to interact with performance counters:
- Use the Create method on the PerformanceCounterCategory class to define a new category containing one or more counters. Similarly, the Delete method removes a user-defined category and the counters it contains.
- Use the Exists method on the PerformanceCounterCategory class to determine if a category of counters already exists.
- Use the Increment or IncrementBy methods on the PerformanceCounter class to increment a counter value by one or by the specified integer and to return the new value of the counter. Similarly, you can use the Decrement method to reduce a counter's value.
- Use the NextValue method on the PerformanceCounter class to return the next calculated value for the sample, or the NextSample method to return the next sampling.
- Use the GetCounters method on the PerformanceCounterCategory class to return an array of counters in a category.