8

我想阅读NextValue()“ASP.NET”性能计数器类别的性能。但是,此类别的计数器始终显示 0,而其他计数器按预期工作。

远程计算机上 perfmon.exe 中的“ASP.NET”计数器工作正常。

我的本地计算机上针对远程计算机的 perfmon.exe 中的“ASP.NET”计数器也显示为 0。

var pc = new PerformanceCounter("ASP.NET", "Requests Current", "", "myRemoteMachine");
pc.NextValue(); // returns always 0
pc.NextValue(); // returns always 0

有任何想法吗?权限或某种防火墙问题?

4

2 回答 2

3

解决方案是在调用 NextValue 之间休眠 1 秒。

在 VB 中:

Dim cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total", "servername")

cpu.NextValue()

System.Threading.Thread.Sleep(1000)

MyValue = cpu.NextValue()

很难知道它是否仍然返回正确的数字,但它与 perfmon 显示的非常接近(在 1 分之内)。我也尝试了 2 秒,它似乎更接近 perfmon 显示的内容。

来自http://blogs.msdn.com/b/dotnetinterop/archive/2007/02/02/system-diagnostics-performancecounter-and-processor-time-on-multi-core-or-multi-cpu.aspx

请记住,根据文档,您需要在调用 NextValue() 之间延迟“大约 1 秒”!

...以及指向https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx的链接,其中指出:

If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.

于 2015-04-20T19:53:51.617 回答
1

If sleep calls between .NextValue() don´t work, you must put your user in the "Performance Monitor Users" group permission.

If it is a service, put user "Local Service" in the "Performance Monitor Users" group.

How to add user to Performance Monitor Users group

于 2020-06-26T19:27:57.587 回答