Reputation: 149
I want to read the performance NextValue()
's of the "ASP.NET" performance counters category. However the counters of this category are always showing 0, whereas other counters work as expected.
The "ASP.NET" counters in perfmon.exe on the remote machine are working fine.
The "ASP.NET" counters in perfmon.exe on my local machine targeting the remote machine also show 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?
Upvotes: 8
Views: 1655
Reputation: 11
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.
Upvotes: 1
Reputation: 4742
The solution is to sleep 1 second between the calls to NextValue.
In 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. I tried it with 2 seconds also and it appears to be a bit closer to what perfmon shows.
Keep in mind that you need to delay "about 1 second" between calls to NextValue(), as per the documentation!
...and links to https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx, which states:
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.
Upvotes: 3