I am attempting this:
using System.Diagnostics;
// ...
var queueCounter = new PerformanceCounter(
"MSMQ Queue",
"Messages in Queue",
@"machinename\private$\testqueue2");
Console.WriteLine( "Queue contains {0} messages",
queueCounter.NextValue().ToString());
Which came from this post: Is there a way to check how many messages are in a MSMQ Queue?
There is mention of this same error, but no resolution when using PerformanceCounter.
I also found mention here: Performance Counter - System.InvalidOperationException: Category does not exist
However, this thread started on this exact topic, but went another direction before answering the initial question on what to do about the error. Basically I don't need to know records per second, I only need to know when a queue starts getting backed up.
What causes this error? I have tried pointing to private and public queue's as well as pointing to queues that had messages in them.
Edit: I have added the counter in perfmon to ensure I have the server path and queue name correct.
Ok... figured it out. The queue names themselves did not include the fully qualified name of the machineName they were running on. I discovered this by using the PerformanceCounterCategory.GetInstanceNames(). This gives you the correct name of the queue. The fix was to new up using the last constructor of PerformanceCounter that lets you specify the machine name. The queue name I specify is the machine name, but the machine name is fully qualified:
new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"<machine name>\private$\dispatch", @"<fully qualified machine name>"))