CRM2011: Useful ITracingService addition 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.

{codecitation style=”brush: c#;”}
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 != “kms_customEntity”)
return;

try
{

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

tracingService.Trace(“KMS Plugin has started..”);
tracingService.Trace(“Start QueryExpression”);

//Query Project Link Settings
QueryExpression query = new QueryExpression(“kms_customEntity2”);
ColumnSet cols = new ColumnSet();
cols.AllColumns = true;
query.ColumnSet = cols;

EntityCollection settings = service.RetrieveMultiple(query);

foreach (Entity pls in settings.Entities)
{
tracingService.Trace(pls[“kms_customEntity2”].ToString());
}

tracingService.Trace(“KMS Plugin has ended..”);

throw new InvalidPluginExecutionException(“Hello, This is from a plugin that Jonathan has created 🙂 “);

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

}
}
}
{/codecitation}

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…

Microsoft Dynamics 2011 - itracingservice for plugin develeopment

13 comments

  • Jonas Dahlkvist /

    Jonathan, the one bit missing here is how do you get the dialog output box to appear? All I get is a whole lot of text files in the Trace folder on the server. Fine for on-premise, but no good online.
    Jonas

  • Hey Jonas – throwing the exception will display the dialog

    throw new InvalidPluginExecutionException()

  • Great tip!

    What I have not figured out is how to generate the trace log entries without having to throw an exception. My wish is that I can use the tracing service to debug my code. Once the code gets into production, visual studio debugging is no longer an option.

    Cheers!

  • Hi mark

    Glad you enjoyed the tip.

    Unfortunately that is the only way to see the log, we have however used text files, log entities to accommodate this.

    • I have exact query like Tip. I need to insert custom messages/log information in trace file without having to throw an exception. I know the the ITracingService cannot do this but could you provide more info on how do use Log entity to log custom info without throwing an exception? Ideally I want the custom log info at the same place as the tracing directory.

      • Hi There

        Other ideas would be to create a log entity within CRM that would show you the error, however you could create a plugin that would write a text file – you could design a system errorlog class and add that to you plugins so it would write to a text file on the server (on premise only)

        You could also looking into the idea of (once again on premise only) writing to the event log on the machine so therefore you have a cleaner way of checking for errors.

        Hope this helps

        Jonathan

  • Do you know if there’s a way to get the Trace message from an SDK app? My app gets the InvalidPluginExecutionException, and can report InvalidPluginExecutionException.Message, but I would also like to have it report the Trace messages.

  • Can this tracing works in custom workflow activity?

  • Qaisar Shabbir Awan /

    have a Plugin on the Creation, Updating, and Deletion of the OpportunityProduct entity in the CRM 2011. I want to through exception on the success of some operations, means want to display dialog. It is working fine for Update and Delete. But it is not working fine for the Creation of OpportunityProduct, it is not throwing exception in the dialog. It is showing Exception: “An error has occurred, Try this action again. If the problem continues, check the Microsoft Dynamics CRM Community for solution or contact your organization’s Microsoft Dynamics CRM Administrator. Finally, you can contact Microsoft Support.”

    Message : Create
    Primary Entity : opportunityproduct
    Action: Pre-Operation
    Execution Mode: Synchronous

  • Trace messages don’t appear in CRM 2013 Online – any ideas?

  • Guys, I launched the exception but I’ve never had the dialog box !! Do you have any idea please?

    Thanks

Leave a Reply

Your email address will not be published.