2012-07-24 98 views
1

有什么办法可以获得性能计数器的集合吗?性能计数器集合

我的意思是,而不是创建几个性能计数器,如

PerformanceCounter actions = new PerformanceCounter("CategoryName", "CounterName1","instance"); 
PerformanceCounter tests = new PerformanceCounter("CategoryName", "CounterName2", "instance"); 

我想获得一个集合(类别名称),其中每个项目将是一个CounterName项目。

所以没有必要在单独的柜台创作。

+2

不知道为什么有人downvoted这个问题... – jsmith 2012-07-24 15:33:00

回答

2

按照你的描述我相信你想创建自定义计数器。您可以一次创建计数器,但您必须逐个创建实例。使用CounterCreationDataCollectionCounterCreationData类。首先,创建计数器数据,将它们添加到新的计数器类别,然后创建自己的实例:

//Create the counters data. You could also use a loop here if your counters will have exactly these names. 
CounterCreationDataCollection counters = new CounterCreationDataCollection(); 
counters.Add(new CounterCreationData("CounterName1", "Description of Counter1", PerformanceCounterType.AverageCount64)); 
counters.Add(new CounterCreationData("CounterName2", "Description of Counter2", PerformanceCounterType.AverageCount64)); 

//Create the category with the prwviously defined counters. 
PerformanceCounterCategory.Create("CategoryName", "CategoryDescription", PerformanceCounterCategoryType.MultiInstance, counters); 

//Create the Instances 
CategoryName actions = new PerformanceCounter("CategoryName", "CounterName1", "Instance1", false)); 
CategoryName tests = new PerformanceCounter("CategoryName", "CounterName2", "Instance1", false)); 

我的建议是不要使用通用名称作为计数器名称。创建计数器之后,您可能想要收集其数据(可能是通过性能监视器),因此而不是使用CounteName1作为计数器表示的名称(例如,动作,测试...)。

编辑

为了得到一个特定类别的所有计数器一次创建计数器类的一个实例,并使用GetCounters方法:

PerformanceCounterCategory category = new PerformanceCounterCategory("CategoryName"); 
PerformanceCounter[] counters = category.GetCounters("instance"); 

foreach (PerformanceCounter counter in counters) 
{ 
    //do something with the counter 
} 
+0

实际上不是, 我不想创建自定义计数器,但我想使用内置计数器的应用程序。 因此,我不希望将每个性能计数器作为独立单元来创建,而是希望一次获得某个类别名称的所有数据(计数器)......是否有可能? 谢谢 – Igal 2012-07-25 11:07:52

+1

@ user301639请检查我编辑的答案。我希望这是你想要的。 – Schaliasos 2012-07-25 11:35:14

+1

太棒了,这是我寻找的方式。 谢谢 – Igal 2012-08-01 12:08:10