Define w3wp System.Diagnostics.Process for this application pool

I have several websites running on my server.

I have a “diagnostic” page in the application that shows the amount of memory for the current process (very useful).

Now this application is “linked” to another application, and I want my diagnostic page to be able to display memory for another w3wp process.

To get the amount of memory, I use simple code:

var process = Process.GetProcessesByName("w3wp"); string memory = this.ToMemoryString(process.WorkingSet64); 

How can I define my second w3wp process knowing its application pool?

I found the appropriate thread, but could not find a suitable answer: A reliable way to see statistics on specific processes in the IIS6 application pool

thanks

+4
source share
2 answers

You can use WMI to determine which application pool this w3wp.exe process w3wp.exe :

 var scope = new ManagementScope(@"\\YOURSERVER\root\cimv2"); var query = new SelectQuery("SELECT * FROM Win32_Process where Name = 'w3wp.exe'"); using (var searcher = new ManagementObjectSearcher(scope, query)) { foreach (ManagementObject process in searcher.Get()) { var commandLine = process["CommandLine"].ToString(); var pid = process["ProcessId"].ToString(); // This will print the command line which will look something like that: // c:\windows\system32\inetsrv\w3wp.exe -a \\.\pipe\iisipm49f1522c-f73a-4375-9236-0d63fb4ecfce -t 20 -ap "NAME_OF_THE_APP_POOL" Console.WriteLine("{0} - {1}", pid, commandLine); } } 
+6
source

You can also get the PID using the IIS ServerManager component; the path if you need to access it in code, without redirecting and parsing console output;

 public static int GetIISProcessID(string appPoolName) { Microsoft.Web.Administration.ServerManager serverManager = new Microsoft.Web.Administration.ServerManager(); foreach (WorkerProcess workerProcess in serverManager.WorkerProcesses) { if (workerProcess.AppPoolName.Equals(appPoolName)) return workerProcess.ProcessId; } return 0; } 
+5
source

Source: https://habr.com/ru/post/1299719/


All Articles