有没有办法获得性能计数器的集合?
我的意思是,而不是创建几个性能计数器,例如
PerformanceCounter actions = new PerformanceCounter("CategoryName", "CounterName1","instance");
PerformanceCounter tests = new PerformanceCounter("CategoryName", "CounterName2", "instance");
我想获得一个集合(对于CategoryName),其中每个项目都是CounterName项目。
所以没有必要在单独的柜台创作。
答案 0 :(得分:2)
根据您的描述,我相信您想要创建自定义计数器。您可以一次创建计数器,但必须逐个创建它们的实例。
使用CounterCreationDataCollection和CounterCreationData课程。首先,创建计数器数据,将它们添加到新的计数器类别,然后创建它们的实例:
//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
}