15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

484 ❘ ChaPTer 19 instrumentAtiOn<br />

Performance Counter builder<br />

The sample application PerformanceCounterDemo is<br />

a simple Windows application with just two buttons<br />

to demonstrate writing performance counts. With<br />

the h<strong>and</strong>ler of one button, a performance counter<br />

category is registered; with the h<strong>and</strong>ler of the other<br />

button, a performance counter value is written. In a<br />

similar way to the sample application, you can add<br />

performance counters to a Windows Service (see<br />

Chapter 25, “ Windows Services ” ), to a network<br />

application (see Chapter 24, “ Networking ” ), or to<br />

any other application from that you would like to<br />

receive live counts.<br />

Using Visual Studio, you can create a new<br />

performance counter category by selecting the<br />

performance counters in the Server Explorer <strong>and</strong> by<br />

selecting the menu entry Create New Category on<br />

the context menu. This launches the Performance<br />

Counter Builder (see Figure 19 - 7).<br />

figure 19-7<br />

To create a performance counter category with Visual Studio, Visual Studio must be<br />

started in elevated mode.<br />

Set the name of the performance counter category to Wrox Performance Counters . The following table<br />

shows all performance counters of the quote service.<br />

name desCriPTion T y P e<br />

# of button clicks Total # of button clicks NumberOfItems32<br />

# of button clicks/sec # of button clicks in one second RateOfCountsPerSecond32<br />

# of mouse move events Total # of mouse move events NumberOfItems32<br />

# of mouse move events/sec # of mouse move events in one second RateOfCountsPerSecond32<br />

The Performance Counter Builder writes the confi guration to the performance database. This can also<br />

be done dynamically by using the Create() method of the PerformanceCounterCategory class in the<br />

System.Diagnostics namespace. An installer for other systems can easily be added later using Visual<br />

Studio.<br />

The following code snippet shows how a performance category can be added programmatically.<br />

With the tool from Visual Studio, you can only create a global performance category that doesn ’ t<br />

have different values for different processes of running applications. Creating a performance category<br />

programmatically allows you to monitor performance counts from different applications, which is also<br />

done here.<br />

First, a const for the category name is defi ned, as well as SortedList < TKey, TValue > , which contains the<br />

names of the performance counts:<br />

private const string perfomanceCounterCategoryName = "Wrox Performance Counters";<br />

private SortedList < string, Tuple < string, string > > perfCountNames;<br />

code snippet PerformanceCounterDemo/MainWindow.xaml.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!