CRM 2011/2013 – using ITracingServicing and Mocking it up

I was writing a plugin and I wanted to step through the code whilst writing it but we had a looming deadline and I didn’t have time to write some unit tests, so I decided just to access the plugin console app.

The other plugins were using the ITracingService and I wanted to keep it similar but this ended up causing me a little bit of a problem.

You will often see ITracingService initialized in plugins like this

// Obtain the tracing service from the service provider.

this.TracingService = (ITracingService)serviceProvider.GetService(typeof (ITracingService));

if you use the CRM Developer toolkit you will see this

IPluginExecutionContext context = localContext.PluginExecutionContext;

What is happening here is it does the line above in the Plugin class.  The plugin class does a bunch of donkey work you have to do in all plugins, like get the CRM Service, extending IPlugin.  and the all important method

void Execute(IServiceProvider serviceProvider);


New to Plugin Development

if you are new to writing plugins, I recommend you check out my blog posts and video on the subject of plugins

video – Setting up Developer Toolkit for Microsoft Dynamics CRM

blog – Setting up visual studio with the developer toolkit

video – CRM 2013 – Create a simple plugin in CRM 2013 using the CRM Development Toolkit

blog – CRM 2013 – simple update plugin


Getting starting with Tracing in plugins

Whilst searching for information about the Tracing, I found a blog I had written back in Feb 2011!!!

CRM 2011 – You can now add tracing to your plugins

This is also a good blog on getting started with tracing in plugins


I know this post is about creating a mock TracingService but whilst I’m here I might as well explain what tracing is used for.


Tracing is used to log statements (progress) in your plugins and then if there is an error and you throw a InvalidPluginExecutionException, you see you tracing lines in the error.  This is useful for the basic plugin debugging where you can put in numbered tracing lines and then see what stage you have got to before the InvalidPluginExecutionException was thrown.

here you can see the screen shot from inogic, you can see the tracing was used to show what stage the plugin had reached and some values


Back to my problem – creating a Mock tracing object

The reason I needed to create a tracing service because our plugins used the tracing service to log messages and it had a check if it was null and then threw an error.

So I needed to create a TracingService but unfortunatly I could just create a new TracingService e.g.

TracingService tracingService = new TracingService();

TracingService doesn’t roll that way people.


To get round the problem , I created a mock tracing service which implemented the TracingService interface and then I would be able to create a new object.

To view the ITracingService interfaced, I right clicked and went to definition, it will display the interface below

[code language="csharp"]
namespace Microsoft.Xrm.Sdk
    public interface ITracingService
        void Trace( string format, params object[] args);


So I only had one method to stub and in my example I didn’t want to do anything my code called the trace method.

Looking at the trace has a format and then some objects. The code uses this the message is the string format and that is really the only bit I was interested in. So now my MockTracing object prints the Trace text out to the output window.

[code language="csharp"]
  class MockTracing : ITracingService

    public void Trace(string format, params object[] args)



then I thought perhaps it would useful to show the messages in the Output window in visual studio

[code language="csharp"]
    public void Trace(string format, params object[] args)


This worked quite nicely because I could see my progress without throwing an error. I was also able to step through the code.So I finally got to step through my plugin code in my console app and I refreshed my mind on how tracing works in CRM with the added bonus of improving my understanding of tracing in CRM plugins.


4 thoughts on “CRM 2011/2013 – using ITracingServicing and Mocking it up

  1. Raj October 16, 2014 / 12:26 am

    there seems to be something weird happening with the markdown. unable to view the formatted C# code.


    • Hosk October 16, 2014 / 9:57 am

      Thanks for the comment, yes the code seemed to go bonkers but I have hopefully fixed that now


  2. Tony January 14, 2016 / 6:28 pm

    So, how this code can be used with plugins and where is the output displayed?


Leave a Reply

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.