c# - ASP.NET 性能计数器总是返回 0

标签 c# asp.net vb.net

我想读取“ASP.NET”性能计数器类别的性能 NextValue()。然而,该类别的计数器始终显示 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

有什么想法吗?权限或某种防火墙问题?

最佳答案

解决方案是在调用 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 :

Keep in mind that you need to delay "about 1 second" between calls to NextValue(), as per the documentation!

...以及指向 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.

关于c# - ASP.NET 性能计数器总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543479/

相关文章:

c# - 二维码定制尺寸

c# - 如何使用 Linq 按长度对数组进行排序,然后按值排序

c# - GetFiles 在网络驱动器上太慢

c# - (.NET) 生成随机、易记密码的简单方法

c# - ASP.Net 列表框 : maintain listbox scrolling position after partial postback (not site scrolling position! )

.net - .Net 中的图形错误图像插值

vb.net - 计算从某个日期到当前日期耗时(不包括周末)

c# 查询 ms 对 sql server 的 Access

c# - 检测字符串中的垃圾字符

asp.net 渲染折线图