.NET Framework: Reading and Writing to Performance Counters
This sample shows some how to read from and write to Performance Counters
using Visual Basic.NET. These counters can be used to track the performance of
various parts of the operating system and .NET applications.
Featured Highlights:
This how-to demonstrates how to read from and write to performance counters.
Concepts demonstrated are:
-
Referencing an existing performance counter, and reading its value.
-
Creating a custom performance counter that can be written to as well as read
from.
-
Determining via code whether a performance counter is custom or built-in.
Requirements:
Microsoft Visual Studio.NET Professional or greater.
Windows 2000 or Windows XP.
Running the Sample:
To run the sample:
-
Doubleclick the How-To Reading and Writing to Performance Counters.sln
file.
-
Create a custom performance counter that can be written to. Creating an example
counter is outlined below.
-
Open the Server Explorer
-
Select Servers -> your computer name -> Performance
Counters
-
Right click Performance Counters and select "Create New
Category..."
-
In the dialog box, enter " How-To Performance Counters"
as the Category Name, and any description you would like in the Category
Description text box.
-
Click "New" to add a new Performance Counter.
-
Enter "My First Custom Counter" as the Name,
"NumberOfItems32" as the Type, and any
description you would like as the Counter Description.
-
Run the project, or press the "Refresh" button if it is already running.
-
Explore selecting and reading from the built-in performance counters.
-
Select the custom performance counter you build earlier. Select it by Category
and Counter, then use the increment and decrement buttons to increment and
decrement the value.
-
Explore Performance Counters further by creating more custom performance
counters of other types, including "RateOfCountsPerSecond32"
which measures the number of increments that occur in running one-second
intervals.
See Also:
Introduction to Monitoring Performance Thresholds,
System.Diagnostics.PerformanceCounter,
System.Diagnostics Namespace