CRM 2011 – You can now add tracing to your plugins

Posted on February 8, 2011

8


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