Tuesday, May 8, 2012

Use log4javascript with Dynamics CRM 2011

If you’ve been using the Jscript alert function to display the value of variables in your Dynamics CRM 2011 form scripts, or find that using the F12 debugging tools are often cumbersome, consider using log4javascript for an alternative approach to analyzing and debugging code.  log4javascript is a logging framework that is easy to add to CRM as a Web Resource.  Within your code, simply add log statements for anything you want to output and log4javascript will display the log output in a separate window.  You can even filter on different levels of logging so that you see only errors, warnings, or everything.


-          Click Files link, then click log4javascript link

-          Download the zip file, save to disk, and extract file

Adding log4javascript as a Web Resource in CRM 2011:

-          Create a script Web Resource and select log4javascript.js.  My recommendation is to use the name “new_/Common/log4javascript.js” as the Web Resource name.  This will place the script in a path under “Common” where you can also store other common libraries such as jquery and json.

-          You should also add the stub version of log4javascript into CRM as a second Web Resource. The stub version of the file contains only the log4javascript function declarations.  The stub version allows you to keep your log statements in place in your CRM form scripts but turn them off by replacing the use of the full log4javascript.js Web Resource with the stub version.  To add the stub version, creating another Web Resource named new_/Common/log4javascript_stub.js.

-          Publish customizations

Using log4javascript:

-          In a form where you would like to log output, add log4javascript.js to the form’s libraries.

-          In a form’s script, add the following statement to the top of the form or in a location outside script functions:   var log = log4javascript.getDefaultLogger();

-          In places in your code where you want to see the value of variables, enter a statement such as the following:   log.info("MyMethodName: clientName=" + clientName);

-          Publish the form
Now, as your form script executes, you’ll see logging output in a separate window, with the ability to filter, search and sort the logging output.  The output also includes the execution time (to the second) and this can also help identify script code that you might need to optimize.

No comments:

Post a Comment