Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PerformanceCounters on .NET 4.0 & Windows 7

I have a program that works fine on VS2008 and Vista, but I'm trying it on Windows 7 and VS2010 / .NET Framework 4.0 and it's not working. Ultimately the problem is that System.Diagnostics.PerformanceCounterCategory.GetCategories() (and other PerformanceCounterCategory methods) is not working. I'm getting a System.InvalidOperationException with the message "Cannot load Counter Name data because an invalid index '' was read from the registry."

I can reproduce this with the very simple program shown below:

class Program {     static void Main(string[] args)     {         foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())         {             Console.WriteLine(pc.CategoryName);         }     } } 

I did make sure I'm running the program as an admin. It doesn't matter if I run it with VS/Debugger attached or not. I don't have another machine with Windows 7 or VS2010 to test it on, so I'm not sure which is complicating things here (or both?). It is Windows 7 x64 and I've tried forcing the app to run in both x32 and x64 but get the same results.

like image 631
Scott Willeke Avatar asked Oct 08 '09 22:10

Scott Willeke


People also ask

What is .NET CLR memory?

The Performance console . NET CLR Memory category includes counters that provide information about the garbage collector.

How do you check the performance of a .NET application?

In the Windows Performance Monitor (Perfmon.exe), the per-application counters are available under the ASP.NET Applications performance object. If there are multiple applications on the server, you specify a particular application instance when selecting a counter to monitor.

What is performance counter in C#?

Performance counters enable us to publish, capture, and analyze the performance data of running code. A performance graph is a two-dimensional plot with one axis indicating time elapsed and the other reporting relevant relative or actual performance statistics.


1 Answers

It seems performance counters were corrupted on my system. Although I didn't follow this post exactly, it led me to the solution. Here is what I did:

In an command prompt with administrator/elevate privileges typed the following:

lodctr /? 

Useful stuff in there...

Then typed:

lodctr /R 

According to the docs from the prior step, this gets windows to rebuild the perf registry strings and info from scratch based on the current registry settings and backup INI files. I have a feeling this is what did the magic. However, next I noticed the .NET performance counters were not there anymore so based on this I typed the following to reload them:

lodctr "C:\Windows\Microsoft.NET\Framework64\v4.0.20506\corperfmonsymbols.ini" 

Note that this path is for .NET Framework 4.0 on x64. You can imagine the path for other variations of the framework/platform. I'm guessing you should always load the counters from the highest version of the .NET framework that you have installed, but that is just a guess.

I hope this helps someone else someday!

like image 159
Scott Willeke Avatar answered Oct 02 '22 23:10

Scott Willeke