Mar 26, 2013

Debugging and trouble shooting of plug-ins

Post on trouble shooting using tracer.

I was thinking of summarising few facts that would be helpful in solving issues of plug-ins. In my list of suggestions, I am also giving links to other blogs that describes the steps descriptively.

1.Test whether plug-in is firing

First thing first; as a practice I always check whether my plug-in is firing before implementing lengthy logics. For that I always develop a very simple plug-in first and test whether it fires. My plug-in is just throwing an exception. For example, check below line;

throw new InvalidPluginExecutionException("Quote Pre update...fired!");

If I see the exception, I am ready to enhance the code.

2. Debug

Debugging is the next popular way of finding whether code works as expected. This consists of few basic steps to get the debugger start for your operation.

a. Make sure you copy your .pdb files along with .dll files to the specific location
    (i.e. Program Files\Microsoft Dynamics CRM\Server\bin\assembly)
b. Attach w3wp.exe to your process in VS. (attach CRMAsyncService.exe for asynchronous operations or workflows)
c. Do the relevant operation for the plug-in that jumps the debugger to your break point.

3. Remote debugging

In order to perform normal debugging you need to have the developer environment in the CRM server itself. If your problem occurs explicitly in production server you may need to do remote debugging which could be a much complex process.

Please read this post that explains how to do it;
http://weblogs.asp.net/pabloperalta/archive/2010/12/01/how-to-remote-debug-dynamics-crm-plugins-and-workflow-assemblies.aspx

4. Write into a log file

In practical world, you are not allowed to change configurations in a production server and that can stop you from remote debugging either. In that case, you can go back to basics. Writing required information to a log file. It’s a matter of adding few lines to your plug-in code. Check below suggested code;

using System.IO;

StreamWriter sw;
sw = File.CreateText(@"C:\Devspace\log\Calcs.txt"); 
try
{
 ........... 
 sw.WriteLine("Value 1 : " + value1);
 sw.WriteLine("Value 2 : " + value2);
 ...........
 ...........
}
catch (Exception e)
{
 sw.WriteLine("Exception: " + e.Message);
 throw e;
}
finally
{
 sw.Close();
}

5. Profiling (Specially for online plugins)

Trouble shooting of CRM online plug-in could be another hurdle. 

Below post explains how to accomplish it through profiling, using latest plug-in registration tool;
http://guruprasadcrm.blogspot.com.au/2011/11/how-to-debug-crm-2011-online-plugin.html

No comments:

Post a Comment