4

I have a Trace entity in my CRM system, and I want to insert records for that entity regardless of whether or not a plugin or workflow activity fails. When real-time plugin/workflow fails, all the data operations that have happened are rolled back, so any inserted Trace records are also rolled back.

I know I can use the ITracingService, but there are many times when end users aren't willing/able to relay the issues on to me when an error occurs.

Does anyone know of a supported way to have the Trace records get inserted outside of the transaction, so they are available whether or not the code succeeds or fails?

BlueSam
  • 1,888
  • 10
  • 17

1 Answers1

2

If you are using Dynamics CRM Online (this is now supported as of the Spring Update.) It works great and automatically clears the log after 1 day.

For on-premises, you have to write them outside of the executing transaction. So, you have a few different options:

  1. Create a new connection to Dynamics CRM using stored credentials (store them in a custom entity or the Plugin Step's Unsecure or Secure Configuration String (recommend you encrypt the password whichever option you choose.)

  2. Use a tool such as NLog (or Log4Net) and log the same as any other .NET application (since online & sandbox are not a limitation.)

  3. Roll your own custom logging solution and write to either disk or a webservice. Make it implement ITracingService (it just has one simple method) and it can be used anywhere you might use CRM's tracing implementation. You can see my quick implementation (for a different purpose) here: https://stackoverflow.com/a/28186429/394978. You'll still need to implement the writing to disk/webservice/sql. Btw, make it write to a web service and technically it could still work in CRM Online.

Community
  • 1
  • 1
Nicknow
  • 7,154
  • 3
  • 22
  • 38
  • What do you mean by "If you are using Dynamics CRM Online (this is now supported as of the Spring Update.) It works great and automatically clears the log after 1 day." I want to record a request url, request body, and response body from a rest service call. – BlueSam Jun 12 '15 at 20:37
  • 2
    If you use the `ITracingService` in Dynamics CRM Online it is saved to a Plug-in Trace entity in CRM, with the latest release of Dynamics CRM Online. You can save it regardless of whether there is an Exception thrown and you don't need to rely on the user to download a log file. – Nicknow Jun 12 '15 at 20:41
  • So the tracing service traces always gets written in the online spring release? Even if there is no error? – Daryl Jun 14 '15 at 12:22
  • @Daryl - Optionally, yes. You can set it to always save, never save, or save on exception. For production, I would suggest using Save on Exception and use Always Save for test/dev. – Nicknow Jun 14 '15 at 17:52