Dynamics – How to Setup Plugin Tracing in Custom Workflow Steps C#

This is likely a side-effect of me being rather new to coding in C# and coding CRM solutions however I came across an issue in that I needed to create some tracing on a plugin however I couldn’t add it as a parameter of the Execute function.

I’ll cover two ways to enable tracing within a custom plugin, the standard way which everyone suggests and a workaround which I used –

Standard Method

The standard method is to use “IServiceProvider serviceProvider “as a parameter for the Execute method as below and then to retrieve the Tracing Service from it –

public void Execute(IServiceProvider serviceProvider)
{
    // Obtain execution context from serviceProvider
    IPluginExecutionContext context =
        (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));

    // Method to provide logging
    ITracingService tracingService =
        (ITracingService) serviceProvider.GetService(typeof(ITracingService));

    // Write a trace
    tracingService.Trace("This will appear in the Trace Log");
}

 

Which is great however I needed to do it a slightly different way in my case –

Alternative Method

I was using “CodeActivityContext” as a parameter for Execute due to it being a custom workflow action and as such couldn’t get the TracingService in the same manner, instead I had to retrieve it from the executionContext –

public sealed class Class : CodeActivity
{
    protected override void Execute(CodeActivityContext executionContext)
    {
        // Method to provide logging
        ITracingService tracingService =
            executionContext.GetExtension();

        // Write a trace
        tracingService.Trace("This will appear in the Trace Log");
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s