views:

179

answers:

3

I'm trying to show the number of times an operation occurred since the last performance iteration. I have created a performance counter using the following:

var clearStateCounterData = new CounterCreationData()
{
    CounterName = ClearStateName,
    CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
    CounterType = PerformanceCounterType.CounterDelta32
};

Then I call counter.Increment() in my application but I never see the performance counter value move. Even if I run it multiple times per second.

Is there something special I need or a specific value I need to increment by to get the PerformanceCounter to show something?

Figured it out

I put an example of using this counter in an answer below. Thanks for the help guys.

A: 

This is not enough to create the counter... according to the documentation, you need to create a PerformanceCounterCategory and create an instance of PerformanceCounter. Check out the sample in MSDN : http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

Thomas Levesque
Yeah, I did all that. The counter is showing up in my category. I just can't seem to get any values to display by incrementing the counter. I am guessing that counter.Increment is not the right thing or the only thing that I need to call in this case but I don't see any examples for this counter type available.
spoon16
A: 

Once you create the counter (using the CounterCreationData and the Create in PerformanceCounterCategory), and then create an instance of the counter (using PerformanceCounter), you need to initialize the counter value to get the instance started in Performance Monitor.

Also, make sure you are creating the counter in read-write mode (by passing false to the readOnly argument).

You could try setting RawValue = RawValue, or RawValue = 0 to start it up and see if it appears.

Chris Patterson
A: 

Here is an example that worked for me.

class Program
{
    const string CategoryName = "____Test Category";
    const string CounterName = "Clear State Operations";

    static void Main(string[] args)
    {
        if (PerformanceCounterCategory.Exists(CategoryName))
            PerformanceCounterCategory.Delete(CategoryName);

        var counterDataCollection = new CounterCreationDataCollection();

        var clearStateCounterData = new CounterCreationData()
        {
            CounterName = CounterName,
            CounterHelp = "The number of times the service state has been cleared since the last performance iteration",
            CounterType = PerformanceCounterType.CounterDelta32
        };
        counterDataCollection.Add(clearStateCounterData);

        PerformanceCounterCategory.Create(CategoryName, "Test Perf Counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

        var counter = new PerformanceCounter(CategoryName, CounterName, false);

        for (int i = 0; i < 5000; i++)
        {
            var sw = Stopwatch.StartNew();
            Thread.Sleep(10300);
            sw.Stop();

            counter.Increment();
        }

        Console.Read();
    }
}
spoon16