After some playing around, I’ve finally managed to get my custom performance counter up and running so I thought I’d share the basics.
For IMayBeLate, I want to get an idea of how many emails are being generated. The second version of my site (2.0) isn’t live yet, but I thought it might be a nice metric to see the number of emails, push messages and SMSs being generated when the site does go live. To implement this, I decided to try a custom performance counter as it seems ideally suited.
There are two steps. First, a new category of counters must be created. Secondly, your performance counters are added to that category.
First, create a category and add a counter to it.
1: if (!PerformanceCounterCategory.Exists("IMayBeLate"))
2: {
3: CounterCreationDataCollection counters = new CounterCreationDataCollection();
4:
5: CounterCreationData totalOps = new CounterCreationData();
6: totalOps.CounterName = "# emails sent";
7: totalOps.CounterHelp = "Total number of emails sent by the EmailProcessor";
8: totalOps.CounterType = PerformanceCounterType.NumberOfItems64;
9: counters.Add(totalOps);
10:
11: PerformanceCounterCategory.Create("IMayBeLate", "Counters related to IMayBeLate", PerformanceCounterCategoryType.SingleInstance, counters);
12: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
This counter is a PerformanceCounterType.NumberOfItems64 type, which is one that just holds an increasing value.
Next, create an instance of the performance counter.
1: PerformanceCounter totalEmails = null;
2:
3: totalEmails = new PerformanceCounter("IMayBeLate", "# emails sent", RoleEnvironment.CurrentRoleInstance.Id, false);
4: totalEmails.MachineName = ".";
5: totalEmails.ReadOnly = false;
Once you have created the counter, you can increment its value with ease.
1: totalEmails.Increment();
When you open the Performance Monitor in Windows, you should now be able to select from a new category of counters called IMayBeLate and add it.
I plan on deploying my application to Windows Azure in the future. This article https://www.windowsazure.com/en-us/develop/net/common-tasks/performance-profiling/ describes how to go about setting up performance Counters in Azure. Hopefully it won’t be too difficult!
EDIT My apologies for the inconsistent code syntax highlighting here – I’m trying out a few different plug-ins for Windows Live Writer.