Tech Xie
Tech Xie

Reputation: 927

RawFraction performance counters in c#

I am having issues using the RawFraction performance counter in my project. All I want is to show a simple fraction of totalActionType1/totalActions.

So totalActions is my Rawbase counter and totalActionType1 is my Rawfraction counter. I increment both the counters by 1 at specific places but how can I =see the ratio. I know I am doing something/everything wrong. The post on msdn is not helpful too.

I can see using the CounterSample to calculate the float value but then how can I show the float value as the counter raw value.

//Here is how I am incrementing the counters:

case CounterType.totalActions:
totalActions.Increment();
break;


case CounterType.totalActionType1:
totalActionType1.Increment();
break;

//Counter Creation:
totalActionType1 = new PerformanceCounter("Fract","RawFractionType", false);
totalActions = new PerformanceCounter("Fract","RawFractionBaseType", false);
var value = totalActionType1.NextValue();

//Counter Setup:
var countActionType1 = new CounterCreationData("RawFractionType", "", CounterType.RawFraction);
var countTotalActions= = new CounterCreationData("RawFractionBaseType", "", CounterType.RawBase);
categoryCollection.Add(countActionType1 ); 
categoryCollection.Add(countTotalActions);
PerformanceCounterCategory.Create("Fract", "", PerformanceCounterCategoryType.SingleInstance, categoryCollection);

Thanks,

Upvotes: 2

Views: 1318

Answers (1)

James Michael Hare
James Michael Hare

Reputation: 38427

So, I assume you created your counters like as follows in your installer class. It's important that the base counter immediately follow the calculated counter.

            installer.Counters.Add(
                new CounterCreationData(counterName, counterDescription,
                                        PerformanceCounterType.RawFraction));
            installer.Counters.Add(
                new CounterCreationData(counterName + "-Base",
                                        counterDescription,
                                        PerformanceCounterType.RawBase));

If so, you can then query it by creating a PerformanceCounter instance for the RawFraction and calling NextValue() on it.

// for read-only access to it
var pc = new PerformanceCounter(categoryName, counterName, true);

var value = pc.NextValue();

For some counter types, you have to initially call NextValue() twice, to prime the calculation. Also, keep in mind that RawFraction displays as a percentage, so if the value is 0.40 it will display as 40.0.

Upvotes: 1

Related Questions