Get Performance Counter Instance Name (w3wp#XX) from ASP.NET worker process ID

后端 未结 5 1636
死守一世寂寞
死守一世寂寞 2020-12-31 06:23

I would like to display some memory statistics (working set, GCs etc.) on a web page using the .NET/Process performance counters. Unfortunately, if there are multiple applic

5条回答
  •  Happy的楠姐
    2020-12-31 07:25

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

提交回复
热议问题