Boas Enkler
Boas Enkler

Reputation: 12557

Getting Performance Counters from W3wp in c#

I want to retrieve performance Counters from a W3WP (IIS 10.0) Process. I', able to read Process and Thread Performance counters, but not any other like % Time spent in GC

So I wrote a small console showing me all Categories which may have a associations with w3wp and I only see Process and Thread.

I also ran the console as administrator and as the user with which the app pool is running. But still same result.

What am I missing?

        var cats = PerformanceCounterCategory.GetCategories();

        foreach (var cat in cats)
        {

            var names = cat.GetInstanceNames();

            if (names.Any(i => i.ToLowerInvariant().Contains("w3wp")))
            {
                Console.WriteLine(cat.CategoryName);
            }
        }



        private static string GetProcessInstanceName(int pid)
        {
            PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");

            string[] instances = cat.GetInstanceNames();
            foreach (string instance in instances)
            {

                using (PerformanceCounter cnt = new PerformanceCounter("Process",
                     "ID Process", instance, true))
                {
                    int val = (int)cnt.RawValue;
                    if (val == pid)
                    {
                        return instance;
                    }
                }
            }
        }

Upvotes: 1

Views: 1314

Answers (1)

Kevin Holditch
Kevin Holditch

Reputation: 5303

You have to run the application in the same bitness as your website. If your website is 64 bit then you need to run the application in 64 bit mode.

To do this right click on the console application project, click properties in the Build tab untick the box that says "Prefer 32-bit".

See Reading w3wp .Net Performance Counter Instances programmatically for more info.

Upvotes: 1

Related Questions