gpt4 book ai didi

c# - 递增自定义性能计数器 .net 4.0 Windows 7

转载 作者:行者123 更新时间:2023-11-30 18:04:22 26 4
gpt4 key购买 nike

我在 Windows 7 上增加自定义性能计数器时遇到一点问题。创建了类别,创建了计数器,但调用“Increment()”或设置“RawValue”似乎没有效果。例如,Increment() 的返回值仍然是 0L。

以下代码是一个控制台应用程序,需要以管理员身份运行才能创建类别。调试通过显示上述症状。

非常感谢任何帮助。

class Program
{
private static string _category = "MyCustomCounters";

static void Main(string[] args)
{
var deleteIfExists = false;

if (args.Any())
{
if (args.Count() > 0)
{
_category = args[0];
}

if (args.Count() > 1)
{
deleteIfExists = args[1].ToUpper() == bool.TrueString.ToUpper();
}
}

CreateCustomCounters(_category, deleteIfExists);
}

private static void CreateCustomCounters(string category, bool deleteIfExists)
{
if (deleteIfExists && PerformanceCounterCategory.Exists(category))
{
PerformanceCounterCategory.Delete(category);
}

if (!PerformanceCounterCategory.Exists(category))
{
CounterCreationDataCollection counterCollection = new CounterCreationDataCollection();

// add a counter tracking operations per second
CounterCreationData opsPerSec = new CounterCreationData();
opsPerSec.CounterName = "# requests /sec";
opsPerSec.CounterHelp = "Number of requests executed per second";
opsPerSec.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
counterCollection.Add(opsPerSec);

// add a counter tracking operations per second
CounterCreationData operationTotal = new CounterCreationData();
operationTotal.CounterName = "Total # requests";
operationTotal.CounterHelp = "Total number of requests executed";
operationTotal.CounterType = PerformanceCounterType.NumberOfItems32;
counterCollection.Add(operationTotal);

PerformanceCounterCategory.Create(category,
"A custom counter category that tracks execution", PerformanceCounterCategoryType.SingleInstance,
counterCollection);
}
else
{
Console.Error.WriteLine("Counter already exists, try specifying parameters for categoryname and or insisting on deleting");
}

using (var _totalOperationsCounter = new PerformanceCounter(_category, "Total # requests", false))
{
_totalOperationsCounter.ReadOnly = false;
_totalOperationsCounter.RawValue = 10;
var count = _totalOperationsCounter.Increment();
}
}
}

最佳答案

对我来说,行为如下(我没有设置 RawValue)

  • 当性能监视器未运行时
    • Console.WriteLine(_totalOperationsCounter.Increment()) 始终显示 1
  • 当性能监视器正在运行时
    • Console.WriteLine(_totalOperationsCounter.Increment()) 在应用程序运行之间递增

关于c# - 递增自定义性能计数器 .net 4.0 Windows 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6220145/

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