Walkthrough: Debugging a SharePoint Application by Using IntelliTrace
By using IntelliTrace, you can more easily debug SharePoint solutions. Traditional debuggers give you only a snapshot of a solution at the current moment. However, you can use IntelliTrace to review past events that occurred in your solution and the context in which they occurred and navigate to the code.
This walkthrough demonstrates how to debug a SharePoint 2010 or SharePoint 2013 project in Visual Studio Ultimate by using Microsoft Monitoring Agent to collect IntelliTrace data from deployed applications. To analyze that data, you must use Visual Studio Ultimate. This project incorporates a feature receiver that, when the feature is activated, adds a task to the Task list and an announcement to the Announcements list. When the feature is deactivated, the task is marked as completed, and a second announcement is added to the Announcements list. However, the procedure contains a logical error that prevents the project from running correctly. By using IntelliTrace, you'll locate and correct the error.
Applies to: The information in this topic applies to SharePoint 2010 and SharePoint 2013 solutions that were created in Visual Studio.
This walkthrough illustrates the following tasks:
|
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE. |
You need the following components to complete this walkthrough:
Supported editions of Windows and SharePoint. See Requirements for Developing SharePoint Solutions.
Visual Studio Ultimate.
First, you create an empty SharePoint project that has a feature receiver.
To create a feature receiver
Create a SharePoint 2010 or SharePoint 2013 solution project, and name it IntelliTraceTest.
The SharePoint Customization Wizard appears, in which you can specify both the SharePoint site for your project and the trust level of the solution.
Choose the Deploy as a farm solution option button, and then choose the Finish button.
IntelliTrace operates only on farm solutions.
In Solution Explorer, open the shortcut menu for the Features node, and then choose Add Feature.
Feature1.feature appears.
Open the shortcut menu for Feature1.feature, and then choose Add Event Receiver to add a code module to the feature.
Next, add code to two methods in the feature receiver: FeatureActivated and FeatureDeactivating. These methods trigger whenever a feature is activated or deactivated in SharePoint, respectively.
To add code to the feature receiver
At the top of the
Feature1EventReceiverclass, add the following code, which declares variables that specify the SharePoint site and subsite:Replace the
FeatureActivatedmethod with the following code:Replace the
FeatureDeactivatingmethod with the following code:
Now that the code is added to the feature receiver and the data collector is running, deploy and run the SharePoint solution to test whether it works correctly.
|
To test the project
Deploy the solution to SharePoint, and then open the SharePoint site in a browser.
The feature automatically activates, causing its feature receiver to add an announcement and a task.
Display the contents of the Announcements and Tasks lists.
The Announcements list should have a new announcement that's named Activated feature: IntelliTraceTest_Feature1, and the Tasks list should have a new task that's named Deactivate feature: IntelliTraceTest_Feature1. If either of these items is missing, verify whether the feature is activated. If it isn't activated, activate it.
Deactivate the feature by performing the following steps:
On the Site Actions menu in SharePoint, choose Site Settings.
Under Site Actions, choose the Manage site features link.
Next to IntelliTraceTest Feature1, choose the Deactivate button.
On the Warning page, choose the Deactivate this feature link.
The FeatureDeactivating() event handler throws an error.
If you install Microsoft Monitoring Agent on the system that’s running SharePoint, you can debug SharePoint solutions by using data that's more specific than the generic information that IntelliTrace returns. The agent works outside of Visual Studio by using PowerShell cmdlets to capture debug information while your SharePoint solution runs.
|
On the computer that's running SharePoint, set up Microsoft Monitoring Agent and start to monitor your solution.
Deactivate the feature:
On the Site Actions menu in SharePoint, choose Site Settings.
Under Site Actions, choose the Manage site features link.
Next to IntelliTraceTest Feature1, choose the Deactivate button.
On the Warning page, choose the Deactivate this feature link.
An error occurs (in this case, because of the error thrown in the FeatureDeactivating() event handler).
In the PowerShell window, run the Stop-WebApplicationMonitoring command to create the .iTrace file, stop monitoring, and restart your SharePoint solution.
Stop-WebApplicationMonitoring "<SharePointSite>\<SharePointAppName>"
Now you can view the IntelliTrace log file in Visual Studio to find and fix the error in the SharePoint solution.
To debug and fix the SharePoint solution
In the \IntelliTraceLogs folder, open the .iTrace file in Visual Studio.
The IntelliTrace Summary page appears. Because the error wasn't handled, a SharePoint correlation ID (a GUID) appears in the unhandled exception area of the Analysis section. Choose the Call Stack button if you want to view the call stack where the error occurred.
Choose the Debug Exception button.
If prompted, load symbol files. In the IntelliTrace window, the exception is highlighted as "Thrown: Serious error occurred!".
In the IntelliTrace window, choose the exception to display the code that failed.
Fix the error by opening the SharePoint solution and then either commenting out or removing the throw statement at the top of the FeatureDeactivating() procedure.
Rebuild the solution in Visual Studio, and then redeploy it to SharePoint.
Deactivate the feature by performing the following steps:
On the Site Actions menu in SharePoint, choose Site Settings.
Under Site Actions, choose the Manage site features link.
Next to IntelliTraceTest Feature1, choose the Deactivate button.
On the Warning page, choose the Deactivate this feature link.
Open the Task list, and verify that the Status value of the Deactivate task is "Completed" and its % Complete value is 100%.
The code now runs properly.
Verifying and Debugging SharePoint Code
IntelliTrace
NIB: Walkthrough: Verify SharePoint Code by Using Unit Tests