Identify the w3wp System.Diagnostics.Process for a given application pool

左心房为你撑大大i 提交于 2019-12-10 15:26:19

问题


I got few web sites running on my server.

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

Now this app is 'linked' to another app, and I want my diagnostic page to be able to display the amot of memory for another w3wp process.

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

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

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

I found a corresponding thread, but no appropriate answer : Reliable way to see process-specific perf statistics on an IIS6 app pool

Thanks


回答1:


You could use WMI to identify to which application pool a given w3wp.exe process belongs:

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



回答2:


You can also get the PID using the IIS ServerManager component; way, 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;
}


来源:https://stackoverflow.com/questions/2160726/identify-the-w3wp-system-diagnostics-process-for-a-given-application-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!