Debugging

Debugging is the process of identifying and resolving errors in a system. Though it is a complex and time-consuming task, it is necessary to debug the code and ensure the functionality of the application. Because a single bug can cause a drastic impact, it might be an iterative process and might require multiple attempts to track and resolve the bugs.

Debugging a Dynamics 365 CRM plugin

Dynamics 365 CRM plugins are developed in C#(.NET). Plugins are of two types, which are synchronous and asynchronous. We cannot debug the plugin on the runtime; rather, we can do it using a profiler. A profiler will capture the instance of the plugin execution and save it as a row in the plugin-profiler table in Dataverse. You need the Plugin Registration tool to debug the Dataverse plugins.

There are multiple ways in debugging Dynamics 365 CRM Plugins.

I have used create email activity plugin to demonstrate the debugging of the plugin. This plugin creates an email activity when a contact record gets created. After the development, build the plugin and sign the assembly.

Trace Logs

We can log the errors using the ITracingService interface in the plugin. This trace log accepts only the string to log. It is used for debugging both synchronous and asynchronous plugins. If you want to log values of any other data type, you should typecast it to string. This will log the errors, and it will appear on the Plugin Trace logs. Before that, you should make sure that your environment enabled the Plugin Trace logs.

ITracingService log = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
log.Trace("Execution Started");

To do so, you should navigate to Settings 🡲 Advanced Settings 🡲 Administration 🡲 System Settings 🡲 Customization 🡲 Enable Plug-in and custom workflow activity tracing to All (if not enabled).

Debug Dynamics 365 CRM Plugin

Persist to Entity (Recommended)

This is the recommended way of debugging Dynamics 365 CRM plugins. In this method, we can use the saved profiles from the plugin profiles table to debug the plugin code. It is used for debugging both synchronous and asynchronous plugins. In this method, we can catch the profiler once the whole process is completed.

Follow the below steps to debug the plugin using Persist to Entity

Debug Dynamics 365 CRM Plugin

Exception

The exception is another way to debug the Dataverse plugins. It will use the exception file as the profile. In this method, we cannot proceed with the whole action even if it is executed in the right way. Because of the system throws an exception and stops the process. This method is used for debugging the synchronous plugins.

Follow the below steps to debug the plugin using the Exception

Note. I have used the same plugin which is demonstrated in Persist to entity method

Summary

Thus, the above-mentioned ways are used for debugging the plugins. But Microsoft recommends using persist to the entity for debugging. Trace logs will be used for logging the values. As programming is not a one-time activity, it requires a lot of debugging to make the right application.

Have a great day!