CRM 2011 – You can now add tracing to your plugins

I have been looking at creating my first plugin in CRM 2011.  As usual with CRM, things are always a bit of mystery until you have created that first working example

I will talk about plugins in future blogs but at the moment I am just going to talk about the ITracingService feature that has been added.

it’s basically a new feature which allows you to put out some tracing whilst you are developing the plugin and if the plugin throws an exception in the code it will not only put out the error but will put out the tracing comments so you can get an idea of how far it got within your plugin code

//Initialize the service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

once you have initialized the service you then just need to create messages in your code

tracingService.Trace(yourMessage);

This webpage has a great explanation of the new ITracingService feature

MSCRM 2011 – Useful ITracingService addiion when creating plugin assemblies.

So i started working on upgrading our plugin assemblies today to work in the isolated mode “Sandbox” for the CRM Online deployments. whilst browsing the SDK i came across a great new tracing tool for debugging the code called ITracingService this service alows a trace log to be generated at runtime. see the following code example and the output.

using System;
using System.ServiceModel;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
namespace Kms.Xrm.Plugin.OnlineTestPlugin
{
 public class FollowupPlugin: IPlugin
 {
 /// <summary>
 /// A plugin that creates a follow-up task activity when a new account is created.
 /// </summary>
 /// <remarks>Register this plug-in on the Create message, account entity,
 /// and asynchronous mode.
 /// </remarks>
 public void Execute(IServiceProvider serviceProvider)
 {
 //Extract the tracing service
 ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 if (tracingService == null)
 throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");

 // Obtain the execution context from the service provider.
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 // The InputParameters collection contains all the data passed in the message request.
 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
 {
 // Obtain the target entity from the input parmameters.
 Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
 // If not, this plug-in was not registered correctly.
 if (entity.LogicalName != "account")
 return;
try
 {
// Obtain the organization service reference.
 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
tracingService.Trace("01 - Hosk tracing started..");
 tracingService.Trace("02 - about to do query expression");

 //Query Project Link Settings
 QueryExpression query = new QueryExpression(" account ");
 ColumnSet cols = new ColumnSet();
 cols.AllColumns = true;
 query.ColumnSet = cols;

 EntityCollection settings = service.RetrieveMultiple(query);
foreach (Entity ent in settings (plus [s)
 {
 tracingService.Trace(" account name " + ent.attributes["name"] &quot);
 }

 tracingService.Trace("The Plugin tracing has finished ..");
throw new InvalidPluginExecutionException("Hosk account plugin has finished running :) ");

 }
 catch (FaultException<OrganizationServiceFault> ex)
 {
 throw new InvalidPluginExecutionException("An error occurred in the Hosk Test plug-in.", ex);
 }
 }
}
 }
}

The output can be seen below. If coded correctly the end user can supply much more detailed information to support. this obviously saves time on the searching trace files and error messages…

below is an image from a great blog post on tracing from inogic
http://inogic.com/blog/2011/01/use-tracing-in-crm-2011-plugins/

tracing description

About these ads

8 thoughts on “CRM 2011 – You can now add tracing to your plugins

  1. Jonathan nachman January 31, 2012 / 12:00 pm

    Scrap That – looking at it in my browser shows that you have linked….

    I can only apologize, keep up the good work.

    Thanks

    Jonathan

  2. tj September 13, 2012 / 1:47 pm

    how do you get your tracing to show up in the exception dialog? I only get the exception text. I can’t have remote debugging working. The profiing doesn’t work with plug in built in CRM 4.0. I have tested with the sample code created using 5.0 and i was able to profile the plugin and get the encrypted errordetail.txt file. I tried with Tracing, but not sure where is being written. I dont see anything under the Trace directory on the server in the trace file that is most recent. The dialog only shows my exception message, but not the trace message. It is like anything I try doesn’t work. can you help?

    • Hosk September 13, 2012 / 1:55 pm

      if you can get the profile errorDetail.txt then you should be able to step through the code in the debugger using the plugin registration tool

      https://crmbusiness.wordpress.com/2012/06/18/crm-2011-debugging-plugins-using-the-plugin-registration-tool/

      to turn on tracing messages in the main log file, you need to use Tanguys tools to turn on tracing (and there are different levels) then the log files will fill up very quickly but you will get lots error messages.

      • tj September 13, 2012 / 2:02 pm

        Profiling is only working with the plugin built with 5.0 sdk. My plugins are built in 4.0 sdk and migrated to CRM 2011. Every time I start profiler on it, the errordetails says “Exception in initialization of profiler.” I dont get any encrypted text to be used with Registration tool.

      • Bill Harts July 10, 2013 / 8:58 pm

        I’m having the same problem as tj. How do you get your tracing to show up in the exception dialog? I only get the exception text. Very frustrating? I’ve tried enabling *;Verbose tracing and still no trace data shows up, not in the dialog nor on the screen.

  3. punditor October 31, 2013 / 8:08 pm

    Hi, thank you for nice post about CRM Plugin tracing. It was tricky at the beginning but then it was pretty straight forward. Thank you

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