user1672517
user1672517

Reputation:

How to debug a plugin in on-line version?

I've created a plugin and registered it using hte registration tool. I've also added a step that is supposed to handle a message of creation of an instance. Sadly, the intended behavior doesn't occur.

My guess is that something inside the plugin crashes but I have no idea on how to debug it. Setting up breakpoints is not going to work agains on-line version, I understand, so I'm not even trying.

For legal and technical reasons, I won't be able to lift over the solution to an on-premise installation, neither. Is guessing my only option?

Upvotes: 0

Views: 245

Answers (3)

Greg Owens
Greg Owens

Reputation: 3878

Don't forget that you also have access to the ITracingService.

You can get a reference to it in your Execute method and then write to it every so often in your code to log variables or courses of action that you are attempting or have succeeded with. You can also use it to surface more valuable information when an exception occurs.

It's basically like writing to a console. Then, if anything causes the plug-in to crash at runtime then you can see everything that you've traced when you click Download Log File on the error shown to the user.

Beware though - unless your plug-in actually throws an exception (deliberate or otherwise) then you have no access to whatever was traced.

Example:

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

  // Get a reference to the tracing service.
  ITracingService tracingService = 
    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

  try
  {
    tracingService.Trace("Getting entity from InputParameters...");
    // may fail for some messages, since "Target" is not present

    var myEntity =  (Entity)context.InputParameters["Target"];
    tracingService.Trace("Got entity OK");

    // some other logic here...
  }
  catch (FaultException<OrganizationServiceFault> ex)
  {
    _trace.Trace(ex.ToString());
    while (ex.InnerException != null)
    {
      ex = (FaultException<OrganizationServiceFault>)ex.InnerException;
      _trace.Trace(ex.ToString());
    }
    throw new InvalidPluginExecutionException(
      string.Format("An error occurred in your plugin: {0}", ex));
  }
catch (Exception ex)
{
  _trace.Trace(ex.ToString());
  while (ex.InnerException != null)
  {
    ex = ex.InnerException;
    _trace.Trace(ex.ToString());
  }
  throw;
}
}

Upvotes: 0

user1919730
user1919730

Reputation:

For server-side (plugins) I'm using ITracingService. For client-side I log everything to console. The downside with the first is that you actually need to crash the execution to get to see anything. The downside with the latter is that plugins sometimes get executed without GUI being invoked at all.

When it comes to heavier projects, I simply set up a WCF web service that I call from the plugin and write to that. That way, on one screen, I'm executing the plugin while on the other, I'm getting a nice log file (or just put the sent information to on the screen).

Upvotes: 1

Konrad Viltersten
Konrad Viltersten

Reputation: 39118

You could, for instance, start with a very basic update of a field on the instance of your entity that's being created. When you have that working, you can always fall back to the last working version. If you don't even get that to work, it mean, probably, that you're setting up the plugin registration incorrectly.

A very efficient way would be to lift over the solution to an on-premise version where you have full control but I see in your question that it's not en option.

In case you could lift the solution to an on-premise version, here's a link on how to debug plugins.

Upvotes: 0

Related Questions