Monday 20 March 2017

Tracing in Plugins and Custom Workflow Activities

Sometimes when you develop plugins or custom workflow activities in Dynamics CRM you might not get the expected output and you might wonder what went wrong and where. One way to find out this is debug your code. But there is an easier and light weight way of finding out where the error occurred. That is using tracing.


In system settings under customisation you can enable tracing.

















You can enable it for all or only for exceptions. Normally what I do while developing is enable for all. Once you are done with your code you can set it to Exception.
After you set this settings you need to write to the trace in your code. This is how you do it.
First you need to get hold of tracing service instance. After that you can start writing the values of your variables to the trace.

Example code:

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

namespace Plugin1
{
    public class Class1 : IPlugin
    {
        private const string className = "Class1";
        IOrganizationService service;
        ITracingService tracingService;

        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
            tracingService.Trace("{0} plugin started", Class1.className);

            var inputs = context.InputParameters;
            foreach (var item in inputs)
            {
                tracingService.Trace("{0} Input paramenter. key: {1}, value: {2}", Class1.className, item.Key, item.Value);
            }
        }
    }
}


Once your plugin or custom workflow activity has executed you can check the plugin trace log in CRM and you will see what values are passed and if an error occurred where it occurred and so on.































Note: There is a small delay in displaying trace logs so be patient J

This feature is handy in production environments too to know what went wrong and where?

Depending on requirement you can enable tracing for all or exceptions only. Normally Exceptions only in Production environments.

To clean up the disc space you can schedule bulk deletion jobs to delete old trace logs (e.g. trace logs which are older than 2 days or so)

1 comment:

  1. How to make a bet on a casino | Jackson County Chamber of
    With just four 충청북도 출장마사지 hours to go, the BetMGM casino will allow you to take on 서울특별 출장마사지 two 안성 출장샵 teams 여주 출장샵 of bettors and up to 구리 출장마사지 three players.

    ReplyDelete

How to tackle Concurrent Business Process flows

Dynamics 365 has introduced the new feature of Concurrent Business Process Flows. Here is a couple of good articles on that: http://dev...