gpt4 book ai didi

c# - 性能计数器集合

转载 作者:行者123 更新时间:2023-12-04 05:38:16 32 4
gpt4 key购买 nike

有没有办法获得性能计数器的集合?

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

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

我想获得一个集合(用于 CategoryName),其中每个项目都是一个 CounterName 项目。

所以不需要单独的计数器创建。

最佳答案

根据您的描述,我相信您想创建自定义计数器。您可以一次创建计数器,但必须一一创建它们的实例。

使用 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
}

关于c# - 性能计数器集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11631974/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com