以编程方式读取w3wp .Net性能计数器实例 [英] Reading w3wp .Net Performance Counter Instances programmatically

查看:106
本文介绍了以编程方式读取w3wp .Net性能计数器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用性能工具查看.Net性能计数器时,我可以看到列出的Web进程性能计数器(w3wp,w3wp#1):

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);
}

这是我看到的输出:

请注意,未列出w3wp计数器.有谁知道为什么会这样,我该如何解决?

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

推荐答案

解决方案是您必须以与您的网站相同的身份运行该应用程序.由于我的网站是64位的,因此我需要在64位模式下运行控制台应用程序.为此,请在控制台应用程序项目上单击鼠标右键,在构建"选项卡中单击属性",取消选中首选32位"的框.

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".

此外,当您使用.NET CLR内存类别内的进程ID计数器收集w3wp进程的进程ID时,其开头为零.要获取进程ID,您必须初始化网站并确保至少进行一次垃圾回收.就像在我的测试代码中一样,我可以在Application_Start处理程序中简单地调用GC.Collect.

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.

这篇关于以编程方式读取w3wp .Net性能计数器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆