“Category does not exist.” Creating performance counter for MSMQ Queue

喜你入骨 提交于 2019-12-10 19:51:06

问题


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.


回答1:


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>"))


来源:https://stackoverflow.com/questions/26745176/category-does-not-exist-creating-performance-counter-for-msmq-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!