Creating a new performance counter on Windows with C# .NET

In this post we saw how to list all performance counter categories and the performance counters within each category available on Windows. The last time I checked there were a little more than 27500 counters and 148 categories available on my local PC. That’s quite a lot and will probably cover most diagnostic needs where performance counters are involved in a bottleneck investigation.

However, at times you might want to create your own performance counter. The System.Diagnostics library provides the necessary objects. Here’s how you can create a new performance counter category and a counter within that category:

string categoryName = "Football";
			
if (!PerformanceCounterCategory.Exists(categoryName))
{
	string firstCounterName = "Goals scored";
	string firstCounterHelp = "Goals scored live update";
	string categoryHelp = "Football related real time statistics";
				
	PerformanceCounterCategory customCategory = new PerformanceCounterCategory(categoryName);
	PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.SingleInstance, firstCounterName, firstCounterHelp);
}

You’ll need to run Visual Studio as an administrator to create the category and counter.

Don’t forget to check if the category exists otherwise you’ll get an exception. The new category and counter turns up in the Performance Monitor window:

Custom category and counter creation result

A slightly different way of creating a new counter is through the CounterCreationData object:

PerformanceCounterCategory customCategory = new PerformanceCounterCategory(categoryName);
CounterCreationData counterCreationData = new CounterCreationData(firstCounterName, firstCounterHelp, PerformanceCounterType.NumberOfItems32);
CounterCreationDataCollection collection = new CounterCreationDataCollection();
collection.Add(counterCreationData);
PerformanceCounterCategory.Create(categoryName, categoryHelp, collection);

If you add that counter to the Performance Monitor then there will be no data yet of course.

At first the counter will be set as read-only but it can be changed easily. The PerformanceCounter object has the Increment() and IncrementBy() methods. Increment() adds 1 to the counter value and IncrementBy() increments the value by the specified amount. You can pass a negative value to decrement the current counter value.

Let’s see these methods in action:

string categoryName = "Football";
string counterName = "Goals scored";
PerformanceCounter footballScoreCounter = new PerformanceCounter(categoryName, counterName);
footballScoreCounter.ReadOnly = false;
while (true)
{
	footballScoreCounter.Increment();
	Thread.Sleep(1000);
	Random random = new Random();
	int goals = random.Next(-5, 6);
	footballScoreCounter.IncrementBy(goals);
	Thread.Sleep(1000);
}

Performance Monitor starts putting the values in the graph:

Goals scored performance counter in Performance Monitor

You can view all posts related to Diagnostics here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: