Debugging using Tracing Service in Dynamics 365 CE

Tracing service is an out-of-box service which is provided by Microsoft. Fundamentally, it creates a log in Dynamics 365 CE which helps in debugging the code. This is best suited for non-technical users where coding is not their forte.

Step 1:
Create Plugin. Refer step 1 and 2

Step 2:
Lets code and place traces in the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace TracePlugin
{
    public class TracePlugin : IPlugin
    {
        
            public void Execute(IServiceProvider serviceProvider)
            {
                try
                {
					//Extract the tracing service interface for use in debugging sandboxed plug-ins.
                    ITracingService tracingService =
                                    (ITracingService)serviceProvider.GetService(typeof(ITracingService));
					
					// Obtain the execution context from the service provider.
                    IPluginExecutionContext context = (IPluginExecutionContext)
                        serviceProvider.GetService(typeof(IPluginExecutionContext));
						
					
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
					
					
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    if (context.InputParameters != null)
                    {
						//execute tracing service
                        tracingService.Trace("Tracing Message");
                        Entity account = (Entity)context.InputParameters["Target"];
                        tracingService.Trace("Account Id: " + account.Id.ToString());
                    }

                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
            }
    }
}

Step 3:
Create signing in certificate. Refer to step 4 and 5.

Step 4:
Refer to register plugin dll.

Step 5:
Enable tracing in Dynamics 365 CE.
Setting > Administration > System setting > Customizations > Enable logging to plug-in trace log to ALL > Click Ok

Step 6:
Trigger the event to run the plugin

Step 7: Validate
Setting > Plug-in Tracing log

This is it. We have achieved our goal to debug plugin using tracing.

If you have any issue or concern, comment in the comment section or get connected via LinkedIn. I will be happy to help.

Leave a comment

Design a site like this with WordPress.com
Get started