Reading w3wp .Net Performance Counter Instances programmatically

邮差的信 提交于 2019-12-13 01:54:28

问题


When viewing the .Net performance counters using the Performance tool I can see the web process performance counters listed (w3wp, w3wp#1):

However when I run the following code as Administrator:

var instanceNames = new PerformanceCounterCategory(".NET CLR Memory")
                     .GetInstanceNames()
                     .OrderBy(x => x);

foreach (var name in instanceNames)
{
    Console.WriteLine(name);
}

This is the output I see:

Notice the w3wp counters are not listed. Does anyone know why this is the case and how I can fix it?


回答1:


The solution was you have to run the application in the same bitness as your website. As my website was 64 bit I needed to run the console 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".

Also when you collect the process id for the w3wp process by using the Process ID counter inside the .NET CLR Memory category it is zero to begin with. To get the process id you have to initialize the web site and make sure at least one garbage collection happens. As this was in my test code I could simply call GC.Collect in the Application_Start handler.



来源:https://stackoverflow.com/questions/38072766/reading-w3wp-net-performance-counter-instances-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!