DennyDotNet

Awesome ASP.NET C# and other cool coding stuff

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent posts

Recent comments

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Attaching to W3WP.exe by Process Name and User

A lot of the "attach debugger" macros I found only show you how to attach to the W3WP.exe process for debugging. Well  I run multiple sites under different app pools so I sometimes have multiple W3WP.exe's running and need to debug a specific one. In this case I wanted to attach to a specific IIS process.

First off note that I am using Windows Server 2008 R2 / IIS 7.5 and I have separate application pools for each site with different User Names for each process:

 

So I want to target the "test.testsite.com" process... Here's the Macro code:

(Code is in VB.NET)

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Attach_W3WP
    Public Sub AttachToIIS()
        Try
            Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
            Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
            Dim dbgeng(2) As EnvDTE80.Engine
            dbgeng(0) = trans.Engines.Item("Managed")
            Dim proc2 As EnvDTE.Processes = _
                dbg2.GetProcesses(trans, Environment.MachineName)

            For Each proc As EnvDTE80.Process2 In proc2
                If proc.UserName <> Nothing Then
                    Dim name As String = System.IO.Path.GetFileName(proc.Name)
                    If name = "w3wp.exe" AndAlso proc.UserName.Contains("test.testsite.com") Then
                        proc.Attach2(dbgeng)
                        Exit For
                    End If
                End If
            Next
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

The important line is:

If name = "w3wp.exe" AndAlso proc.UserName.Contains("test.testsite.com") Then

Just replace "test.testsite.com" with the user of the process you want to target.

Run the macro and it should attach to the correct W3WP.exe process.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , ,
Posted by Denny on Thursday, October 29, 2009 5:15 AM
Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed