Asp.net
從 ASP.NET 工作程序 ID 獲取性能計數器實例名稱 (w3wp#XX)
我想使用 .NET/Process 性能計數器在網頁上顯示一些記憶體統計資訊(工作集、GC 等)。不幸的是,如果該伺服器上有多個應用程序池,它們會使用索引(#1、#2 等)進行區分,但我不知道如何將程序 ID(我擁有)與該 #xx 索引匹配。是否有程式方式(來自 ASP.NET 網頁)?
Google上的第一擊:
當多個 ASP.NET 工作程序正在執行時,公共語言執行時 (CLR) 性能計數器的名稱將類似於“W3wp#1”或“W3sp#2”等。這在 .NET Framework 2.0 中得到了糾正,在 .NET CLR 記憶體性能對像中包含一個名為 Process ID 的計數器。此計數器顯示實例的程序 ID。您可以使用此計數器來確定與程序關聯的 CLR 性能計數器。
還有知識庫 281884:
預設情況下,性能監視器 (Perfmon.msc) 通過按以下方式列舉程序來顯示具有相同名稱的多個程序:
程序#1 程序#2 程序#3
性能監視器還可以通過以下方式將程序 ID (PID) 附加到名稱中來顯示這些程序:
程序_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 ..."); }