Updated: An even easier method to do this is to simply launch the debugger by adding a statement to the beginning of the method for Systems.Diagnostics.Debugger.Launch. Thanks to the suggestion I received on the topic - I misplaced the email, so the input will need to remain anonymous...
Updated: Debugging Feature Activated - so I said below that debugging feature activated is easy, which is true especially for site and web scoped features, but may not be obvious if you haven't done it before. To debug FeatureActivated,
1) Properties->SharePoint, change Active Deployment Configuration to No Activation
2) Start the project in the debugger.
3) Activate the feature through site settings (assuming it’s a site or web scoped feature).
Original Post
Recently I had some code that I had to put in feature installed. Debugging FeatureActivated is no problem in VS2010, but FeatureInstalled isn't so simple. The feature installed event gets fired when a feature is installed to the farm. It runs under a higher security context than feature activate, therefore you can do things like write settings to the farm or web application. In my case my feature installed code had a bug, but I wasn't sure how I could hook it. I found one approach at least that did work, so I figured I'd blog about it.
There are three possibilities for debugging the FeatureInstalled (this is for a Farm Solution):
1. The FeatureInstalled is throwing an exception and deploy fails in VS. It's OK that the FeatureInstalled ran partially, and you can rerun to debug.
2. The FeatureInstalled is not throwing an exception, but its OK that it ran once and you can run it again.
3. You can't have the FeatureInstalled event run partially or completely prior to debugging.
The first 2 cases are actually pretty simple. First deploy the project from VS. This will put everything in the right place, even if an exception is thrown in your FeatureInstalled, and it looks like the deploy failed. Then follow the following process.
One way that features are commonly installed is from the command line using STSADM or powershell. You can use this to hook into the debugging process. In this case I will use the "TestFarmSettings" project as an example. I did the following in order to debug the feature installed:
Case 3 its more complicated since you need to get the files into the right places yourself. As a little background, the reason why you run the deploy is to get the solution added and installed, and get the debug information deployed for the gac'd assembly. If you do the add and install solution methods manually, then run the debug files don't get into the right place for the assembly.
You will need to run some manual steps to setup the conditions:
Now you are ready to actually install a feature. Remember that by default farm and web app scoped features will install automatically when the deploy solution step is ran, and so you will need to change this behavior for those solution types. This is controlled by the ActivateOnDefault property for the feature (see Feature Element (Feature))
You will need to copy the debug file (.pdb) into the location that your gac'd assembly gets installed. You can find that location by running dir /s c:\windows\assembly\<your assembly name>.dll. Once the debug file is located beside the assembly, you can then perform the steps outlined with the console app. You will be able to break on the FeatureInstalled event the first time you run it.
If anyone has an easier way, please let me know!