11

I'm trying to read a number of performance counters from a running .NET 4 application, from another .NET 4 application.

Some counters, such as Process\% Processor Time and Process\Private Bytes work fine. However, as soon as I try to read a performance counter from one of the .NET categories, such as .NET CLR Memory\# Gen 0 Collections, I get the following exception:

Instance 'MyApplication' does not exist in the specified Category

When I call:

new PerformanceCounterCategory(".NET CLR Memory").GetInstanceNames()

It returns a very small set of instances, and MyApplication is indeed not in the list. However, when I look at my performance counters in perfmon, the list of instances I see there for the same category/counter is much longer and DOES include MyApplication.

Does anyone know why the .NET counters are not visible to my application?

(Note: The monitored application is started by the monitoring application, so they definitely run in the same user account. I also tried starting my monitoring application as Administrator and adding my account to the Performance Monitor Users group, to no effect.)

Sergey K
  • 4,071
  • 2
  • 23
  • 34
rix0rrr
  • 9,856
  • 5
  • 45
  • 48
  • http://stackoverflow.com/questions/5832709/exception-instance-name-of-instance-does-not-exist-in-the-specified-category – Robert Harvey Jul 04 '11 at 13:55
  • Not the issue; 1 instance only, with and without the XML config it doesn't work, and in any case the counters work properly in perfmon. – rix0rrr Jul 04 '11 at 13:58
  • I only get `"ConsoleApplication2"` when I run the actual generated executable. I don't get it when I run the program from Visual Studio. I get `ConsoleApplication2.vshost` instead. – Robert Harvey Jul 04 '11 at 14:24

1 Answers1

19

There are two settings for a project that can be relevant to your problem.

First and foremost, Project + Properties, Build tab, Platform target. On a machine with a 64-bit operating system, setting this to x86 will get you a list of instance names that do not include 64-bit processes. Perfmon.exe is a 64-bit process, it shows all instances, both 32-bit and 64-bit apps. Get the same behavior by setting the Platform target to AnyCPU, that's not the default anymore on Visual Studio 2010. Untick "Prefer 32-bit" if you see it.

Second is Project + Properties, Debug tab, "Enable the Visual Studio hosting process" option. When checked, you are debugging an process named yourapp.vshost.exe instead of yourapp.exe. That also affects the instance name, it will be yourapp.vshost. Not likely to be the problem in your specific case.

Be sure to update your question with this essential info if this guess wasn't accurate.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2
    It was due to the Platform target. Changing from x86 to 'Any CPU' worked. Thanks so much, I never would have thought of this! – rix0rrr Jul 04 '11 at 15:08
  • Also ensure 'Project Properties -> Build Tab -> Prefer 32 bit' is not set. – Ananke Jul 11 '14 at 13:16
  • 1
    I have the same issue. I tried Any CPU, x86, and x64 (prefer 32 bit is not set). Same error in each situation. Anyone else have this issue? – Roger Nov 05 '14 at 16:04