简体   繁体   English

ASP.NET 性能计数器始终返回 0

[英]ASP.NET Performance Counter always return 0

I want to read the performance NextValue() 's of the "ASP.NET" performance counters category.我想阅读“ASP.NET”性能计数器类别的性能NextValue() However the counters of this category are always showing 0, whereas other counters work as expected.然而,此类别的计数器始终显示为 0,而其他计数器按预期工作。

The "ASP.NET" counters in perfmon.exe on the remote machine are working fine.远程机器上 perfmon.exe 中的“ASP.NET”计数器工作正常。

The "ASP.NET" counters in perfmon.exe on my local machine targeting the remote machine also show 0.我的本地机器上针对远程机器的 perfmon.exe 中的“ASP.NET”计数器也显示为 0。

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

Any ideas?有什么想法吗? Permission or some kind of firewall issue?权限或某种防火墙问题?

The solution is to sleep 1 second between the calls to NextValue.解决方案是在调用 NextValue 之间休眠 1 秒。

In VB:在VB中:

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

cpu.NextValue()

System.Threading.Thread.Sleep(1000)

MyValue = cpu.NextValue()

It's hard to know if it's still returning the correct number, but it's very close (within 1 point) to what perfmon shows.很难知道它是否仍然返回正确的数字,但它与 perfmon 显示的非常接近(在 1 点内)。 I tried it with 2 seconds also and it appears to be a bit closer to what perfmon shows.我也尝试了 2 秒,它似乎更接近 perfmon 显示的内容。

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

Keep in mind that you need to delay "about 1 second" between calls to NextValue(), as per the documentation!请记住,根据文档,您需要在调用 NextValue() 之间延迟“大约 1 秒”!

...and links to https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx , which states: ...以及指向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.如果计数器的计算值取决于两次计数器读取,则第一次读取操作返回 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.重置性能计数器属性以指定不同的计数器相当于创建一个新的性能计数器,使用新属性的第一次读取操作返回 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.建议在调用 NextValue 方法之间的延迟时间为一秒,以允许计数器执行下一次增量读取。

If sleep calls between .NextValue() don´t work, you must put your user in the "Performance Monitor Users" group permission.如果.NextValue()之间的睡眠调用不起作用,您必须将您的用户置于“性能监视器用户”组权限中。

If it is a service, put user "Local Service" in the "Performance Monitor Users" group.如果是服务,将用户“本地服务”放在“性能监视器用户”组中。

如何将用户添加到性能监视器用户组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM