我正在为我的c#项目添加一些性能计数器,并且正在创建一个新的PerformanceCounterCategory。在这个类别中,我想有多个计数器/计时器来跟踪不同的事情。我需要使用多个平均计时器,并尝试了解当CoutnerCreationDataCollection中有多个时,AverageBase计数器如何与正确的AverageTimer32计数器关联。
有几个问题:
1.首先,这是一个正确的方法吗?我发现的样本都只有一个平均计时器
2.如果第一个问题的答案是肯定的,那么我认为第一个AverageBase计数器是在AverageTimer32之后添加到集合中的吗?我确实在文章中找到了一个需要按顺序添加的参考。
这是我想要做的一个例子:
var dataColl = new CounterCreationDataCollection
{
new CounterCreationData
{
CounterType = PerformanceCounterType.AverageTimer32,
CounterName = "AverageTime1",
CounterHelp = "AverageTime1_Help"
},
new CounterCreationData // Does this get linked to AverageTime1 simply
// because it is being added after it?
{
CounterType = PerformanceCounterType.AverageBase,
CounterName = "AverageTime1Base"
},
new CounterCreationData
{
CounterType = PerformanceCounterType.AverageTimer32,
CounterName = "AverageTime2",
CounterHelp = "AverageTime2_Help"
},
new CounterCreationData
{
CounterType = PerformanceCounterType.AverageBase,
CounterName = "AverageTime2_Base"
},
}
PerformanceCounterCategory.Create(
"MyCategoryName"
, "My Category Help"
, PerformanceCounterCategoryType.SingleInstance
, dataColl);
答案 0 :(得分:19)
位置。需要基数的计数器需要立即由定义列表中的基数跟踪。所以你的代码是正确的,你有两个AverageTimer32,每个都跟着AverageBase。
作为旁注,如果您厌倦了一遍又一遍地输入相同的代码,您应该考虑Using XSLT to generate Performance Counters code。