6.13. Implementing Performance Counters that Require a Base Counter

Problem

You need to use some of the more advanced performance counters to accurately track information about your application. This performance counter exists as two counters used together. The first counter is the main counter, which is divided by the second counter, called the base counter. Essentially, the first counter is the numerator and the second counter is the denominator; the custom counter reports the result of this division operation. The main counter is used in tandem with its base counter type to calculate, for example, the average amount of time it takes for an action (e.g., connecting to a server) to complete or the average number of actions that occur during a single process (e.g., database timeouts).

Solution

Create a complex performance counter, which is used in tandem with the base counter type to calculate, for example, the average amount of time it takes for an action to complete or the average number of actions that occur during a single process. Use the following method to create a complex custom counter:

public void CreateComplexCounter(string counterName, string counterHelp, PerformanceCounterType counterType, string baseCounterName, string baseCounterHelp, PerformanceCounterType baseCounterType, string categoryName, string categoryHelp, out PerformanceCounter appCounter, out PerformanceCounter appBaseCounter) { CounterCreationDataCollection counterCollection = new CounterCreationDataCollection( ...

Get C# Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.