我想为系统中可能发生的事件设置PerformanceCounters。我想以百分比(PerformanceCounterType.RawFraction)的形式显示几个不同事件的计数器,但是每个百分比共享相同的分母(PerformanceCounterType.RawBase)。
我认为将一个计数器用作所有RawFraction计数器的RawBase会很容易,但是我不知道如何。
这是一个人为的例子。
我为要计数的事件的不同风格设置了三个计数器:红色,绿色和蓝色。 (想象一下一个具有三个按钮的表单,我们只想计算每个按钮的点击百分比。)
我为事件总数设置了一个计数器:任何
当出现红色时,我增加红色和任意。当出现蓝色时,我增加蓝色和任意。当发生绿色时,我将增加绿色和任意值。
当我尝试将Any计数器重用为常见的RawBase计数器时,出现异常:
System.ArgumentException HResult=0x80070057 Message=Cannot create Performance Category with counter name Any button because the name is a duplicate. Source=System StackTrace: at System.Diagnostics.PerformanceCounterCategory.CheckValidCounterLayout(CounterCreationDataCollection counterData)
设置单独的RawBase计数器来计算许多共享相同分母的RawFraction值似乎过多。有什么建议吗?
这是一些测试代码。
public class PerfCounters
{
private static string CategoryName = "Demo counters";
private static string CategoryHelp = "Demo counters help";
private static string buttonAnyCountName = "Any button";
private static string buttonAnyCountHelp = "Any button help";
private static PerformanceCounter buttonAnyCount;
private static string buttonRedCountName = "% of Red button clicked";
private static string buttonRedCountHelp = "Red button help";
private static PerformanceCounter buttonRedCount;
private static string buttonGreenCountName = "% of Green button clicked";
private static string buttonGreenCountHelp = "Green button help";
private static PerformanceCounter buttonGreenCount;
private static string buttonBlueCountName = "% of Blue button clicked";
private static string buttonBlueCountHelp = "Blue button help";
private static PerformanceCounter buttonBlueCount;
public PerfCounters()
{
if (PerformanceCounterCategory.Exists(CategoryName))
{
// this is a quick and dirty test, so it is easiest to delete and recreate the category on each run
// just remmember to restart Performance Monitor after the counters are created.
PerformanceCounterCategory.Delete(CategoryName);
}
CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();
counterDataCollection.Add(new CounterCreationData(buttonRedCountName, buttonRedCountHelp, PerformanceCounterType.RawFraction));
counterDataCollection.Add(new CounterCreationData(buttonAnyCountName, buttonAnyCountHelp, PerformanceCounterType.RawBase));
counterDataCollection.Add(new CounterCreationData(buttonBlueCountName, buttonBlueCountHelp, PerformanceCounterType.RawFraction));
counterDataCollection.Add(new CounterCreationData(buttonAnyCountName, buttonAnyCountHelp, PerformanceCounterType.RawBase));
counterDataCollection.Add(new CounterCreationData(buttonGreenCountName, buttonGreenCountHelp, PerformanceCounterType.RawFraction));
counterDataCollection.Add(new CounterCreationData(buttonAnyCountName, buttonAnyCountHelp, PerformanceCounterType.RawBase));
// next line throws
// System.ArgumentException - Cannot create Performance Category with counter name Any button because the name is a duplicate.
PerformanceCounterCategory.Create(CategoryName, CategoryHelp, PerformanceCounterCategoryType.SingleInstance, counterDataCollection);
buttonRedCount = new PerformanceCounter(CategoryName, buttonRedCountName, string.Empty, false);
buttonBlueCount = new PerformanceCounter(CategoryName, buttonBlueCountName, string.Empty, false);
buttonGreenCount = new PerformanceCounter(CategoryName, buttonGreenCountName, string.Empty, false);
buttonAnyCount = new PerformanceCounter(CategoryName, buttonAnyCountName, string.Empty, false);
}
public void CountRed()
{
buttonRedCount.Increment();
buttonAnyCount.Increment();
}
public void CountGreen()
{
buttonGreenCount.Increment();
buttonAnyCount.Increment();
}
public void CountBlue()
{
buttonBlueCount.Increment();
buttonAnyCount.Increment();
}
}