In my previous article Plug-in Trace Logs outside the Sandbox, I explained how I came to the conclusion that Plug-in Trace Logs were not being written for non-sandboxed plug-ins. In this article I will describe some code that you can use in your plug-in to manually create those trace logs.
Unsupported code alert
Warning: the following article contains unsupported code and may be hazardous to your (system's) health.
All joking aside, I am using this code to do some investigation for a blog article, so if it fails it's no big deal. I strongly suggest that you don't use this in production!
Show me the code
To make this work, in your plug-in you will need to:
- Add project references to Microsoft.Crm.Sandbox.dll and Microsoft.Crm.Sandbox.Client.dll. Make sure to use versions that are compatible with your SDK dlls.
- Add a using statement for Microsoft.Crm.Sandbox
- Add the following code, which does the same thing that a sandboxed plug-in would do to create a trace log.
N.B. there are many parameters being passed. I have annotated some of them, but there are some that I'm not sure what they are for.
N.B.2 My plug-in was created using Jason Lattimer's CRM Developer Extensions. If yours wasn't then your variable names might be different from mine.
PluginTraceLogRecord pluginTraceLogRecord = new PluginTraceLogRecord( context.OrganizationId, Guid.NewGuid(), // ID for the log record this._unsecureConfig, context.MessageName, context.Depth, context.Mode, (int)PluginTraceLogOperationType.Plugin, 0, // constructor duration DateTime.Now, // constructor start time 0, // execution duration DateTime.Now, // execution start time Guid.Empty, // persistence key - not sure what this is context.PrimaryEntityName, null, // profile - not sure what this is context.RequestId.HasValue ? context.RequestId.Value : Guid.Empty, this._secureConfig, "TheNameOfYourPlugin", // ******** Put your plugin's name here ******** (context.OwningExtension != null) ? context.OwningExtension.Id : Guid.Empty, // plugin step ID "This is some text to trace", "", // exception details context.CorrelationId, false // is system created ); PluginTraceLogQueuesManager.Instance.QueuePluginTraceLogRecord(pluginTraceLogRecord);
It 'works on my machine'™, so hopefully will on yours too. Just to re-iterate, this is totally unsupported, so I strongly suggest that you don't use it in production.