Reading and Publishing Performance Counters in .NET, Page 2
Creating Your Own Performance Counter
Creating your own performance counters uses several classes, all in the
System.Diagnostics namespace. Here's an example that creates two
Private Sub btnRegisterCustom_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRegisterCustom.Click ' Create a custom performance counter If Not PerformanceCounterCategory.Exists("Developer.com") Then Dim ccd1 As New CounterCreationData ccd1.CounterName = "RandomCounter" ccd1.CounterType = PerformanceCounterType.NumberOfItems32 Dim ccd2 As New CounterCreationData ccd2.CounterName = "IncreasingCounter" ccd2.CounterType = PerformanceCounterType.NumberOfItems32 Dim ccds As New CounterCreationDataCollection ccds.Add(ccd1) ccds.Add(ccd2) PerformanceCounterCategory.Create("Developer.com", _ "Developer.com counters", ccds) btnShowCustom.Enabled = True End If End Sub
CounterCreationData class contains the information necessary
to create a single counter: its name and its type. Instances of this calss can
be collected into an instance of the
class. That collection, in turn, can be passed to the
PerformanceCounterCategory.Create method to create both the
category and the counters. Note that .NET doesn't let you add new performance
counters to an existing category.
After running this code, you'll find the new category and the new counters exposed in the Server Explorer tree. You can drag them to the form and work with them just like any other performance counter. However, as things stand, the counters won't have any value. They're waiting for you to create one.
Supplying Values for Performance Counters
There isn't any great trick to supplying your own values for performance counters that you create (note that you can't supply a value for any of the system counters). Here's a bit of code to both modify and display the custom performance counters that I just created:
Private Sub btnShowCustom_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnShowCustom.Click ' Turn on the custom counter display Timer2.Enabled = True End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer2.Tick ' Update and display the custom counters ' First, get the counters Dim pcRandomCounter As PerformanceCounter = _ New PerformanceCounter("Developer.com", "RandomCounter", False) Dim pcIncreasingCounter As PerformanceCounter = _ New PerformanceCounter("Developer.com", "IncreasingCounter", False) ' Now adjust their values pcRandomCounter.RawValue = Int(Rnd(1) * 50000) pcIncreasingCounter.Increment() ' Display in the list box lbPerfData.Items.Add("Random Counter: " & _ pcRandomCounter.NextValue()) lbPerfData.Items.Add("Increasing Counter: " & _ pcIncreasingCounter.NextValue()) End Sub
The trick here is in the constructor for the
class. The three arguments to this particular form of the constructor are the
category name, the counter name, and a boolean that indicates whether the
counter should be opened read-only. Supplying
False for this
boolean indicates that I want a read-write counter. After that, the code can
just set the
RawValue property of the counter, or it can call the
Decrement methods to adjust the value by
1 unit at a time. Figure 4 shows this code in action.
Another Bit of Fit and Finish
I hope you'll agree that working with performance counters in .NET is pretty simple. The next step is to integrate counters into your own applications. While most applications won't need to display the system counters, it can be very helpful to your users to create your own custom counters. For example, suppose you're working on an application to process incoming Web requests for a particular set of files. Would it be helpful to administrators to have counters howing how many requests were successful and how many failed? You bet! And it will only take you a few lines of code to build them.
Mike Gunderloy is the author of over 20 books and numerous articles on development topics, and the lead developer for Larkware. Check out his latest book, Coder to Developer from Sybex. When he's not writing code, Mike putters in the garden on his farm in eastern Washington state.