从ASP.NET工作进程ID获取性能计数器实例名称(w3wp#XX)
时间:2020-03-06 14:31:52 来源:igfitidea点击:
我想使用.NET / Process性能计数器在网页上显示一些内存统计信息(工作集,GC等)。不幸的是,如果该服务器上有多个应用程序池,则使用索引(#1,#2等)来区分它们,但是我不知道如何将进程ID(我拥有)与该#xx索引进行匹配。是否有编程方式(从ASP.NET网页)?
解决方案
在Google上的首次热播:
出现多个CLR性能计数器,其名称类似于" W3wp#1"
When multiple ASP.NET worker processes are running, Common Language Runtime (CLR) performance counters will have names that resemble "W3wp#1" or "W3sp#2"and so on. This was remedied in .NET Framework 2.0 to include a counter named Process ID in the .NET CLR Memory performance object. This counter displays the process ID for an instance. You can use this counter to determine the CLR performance counter that is associated with a process.
以及KB 281884:
By default, Performance Monitor (Perfmon.msc) displays multiple processes that have the same name by enumerating the processes in the following way: Process#1 Process#2 Process#3 Performance Monitor can also display these processes by appending the process ID (PID) to the name in the following way: Process_PID
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; } } } throw new Exception("Could not find performance counter " + "instance name for current process. This is truly strange ..."); }
当我们具有同一个程序的两个版本(名称相同),而一个版本不是.net,并且在非.net版本之后启动.net版本时,chiru的示例在特定情况下不起作用。 .Net版本将被命名为applicaion#1,但是当我们使用此名称访问CLR性能计数器时,计数器上的实例名称将带有不带#1的名称,因此我们会失败。
缺口。
即使更改注册表设置看起来很容易,但是不幸的是,我们大多数人没有权限在服务器上进行更改(或者我们不想触摸它!)。在这种情况下,有一个小的解决方法。我在这里写过博客。