I’m a big fan of unit testing your custom code to prevent errors from ever occuring in the system. However it is not unlikely that QA can still report an issue that is related to your custom code. You will have to ask QA for a replication log, the replication log should describe the specific steps they performed in order for you to replicate the behaviour.
Using the plug-in profiler we can easily step-through our custom code to determine exactly which line of code is causing the error.
What is the plug-in profiler
The plug-in profiler is a tool that profiles the execution of plug-ins and custom workflow activities for an enhanced debugging experience in Microsoft Visual Studio. The tool can be run from the Command Prompt window or from within the Plug-in Registration tool.
Using this tool you also have the ability to replay your plugin-in or custom workflow activity remotely (when disconnected from Dynamics). This is especially useful for ISV providers. They can ask the customer to profile the execution of failing code and you can easily replay this execution on your own system (offline).
Steps to profile the execution of failing code
As mentioned previously, you could use the command prompt (PluginProfiler.Debugger.exe) to profile the execution but I prefer to use the Plugin Registration Tool. Using the command prompt is out of scope of this blog post.
Installing the plug-in profiler
Open your Plugin Registration Tool and connect to a Dynamics organization. Next, install the Profiler solution in our organization (can be either on-premises or online). This can be done by a simple click on a button, “Install Profiler” found in the Plugin Registration Tool.
After the plug-in profiler is installed, you should find a new managed solution installed in your Dynamics system (check out Settings > Solutions to verify).
Next step depends on the type of custom-code you would like to debug. Is it a plug-in or a custom workflow activity? We will start to describe the steps to profile a custom workflow activity. Steps to profile a plugin, can be found right after.
Profile a Custom Workflow Activity
When you want to debug a custom workflow activity you have to find out which failing workflow calls your custom workflow activity. You can also create a new on-demand workflow that calls your custom workflow activity.
When you have found the workflow (or created a new one), we have to start profiling it. In the Plugin Registration Tool select the Plug-in Profiler and click Profile Workflow.
Next, select your workflow (in the drop down list) and check the specific step(s) of the workflow you would like to profile.
Select Exception as profile storage and include secure configuration and click OK. The profile configuration is now stored under plug-in profiler:
The plugin registration tool has changed the workflow by appending “(Profiled)” to the name. You will have to make sure this workflow is a realtime workflow in order for the exception to popup. You can always convert your workflow to a realtime workflow.
Next, we have to trigger the execution of the workflow. A business process error should popup and you can click “Download Log File” to save the (compressed) profile.
Now we have gathered all the necessary information to start debugging our custom code. Next we have to replay the execution to step through our custom code (see replay execution).
When you would like to profile a plugin, the steps are a little different:
Profile a plugin
To profile a plugin, we have to select the specific plugin step that we would like to profile and click “Start Profiling”.
Next, trigger the plugin and the following error should popup:
Download the log file and we are ready to replay the execution.
Replay the execution
Select debug in the plugin registration tool (or “replay plug-in execution” when offline).
Select the file containing your profile and your local (debug) assembly file. Before clicking on “Start Execution”, we have to attach Visual Studio to the Plugin Registration Tool.
In Visual Studio, select “Debug” and “Attach to Process…” (or CTRL + ALT + P). Select the PluginRegistration.exe process and click Attach.
When attached to the plugin registration tool you can start to put breakpoints in your code (Visual Studio). After clicking “Start Execution” in the plugin registration tool, the Visual Studio debugger will popup and break at your specific breakpoint(s). This way you can step through your custom code and find the cause of your issue much faster.
Enjoy the power of the plugin profiler!