CRM 2011 - ITracingService getting access to the traceInfo at runtime

北城以北 提交于 2019-12-23 05:31:29

问题


I have some custom logging in my plugin and want to include the contents of my tracingService in my custom logging (which is called within a catch block, before the plugin finishes).

I cant seem to access the content of tracingService. I wonder if it is accessible at all?

I tried tracingService.ToString() just incase the devs had provided a useful overload, alas as expected I get name of the class "Microsoft.Crm.Sandbox.SandboxTracingService".

Obviously Dynamics CRM makes use of the tracingService content towards the end of the pipeline if it needs to.

Anybody have any ideas on this?

Kind Regards, Gary


回答1:


The tracing service does not provide access to the trace text during execution but that can be overcome by creating your own implementation of ITracingService. Note, you cannot get any text that was written to the trace log prior to the Execute method of your plugin being called - meaning if you have multiple plugins firing you won't get their trace output in the plugin that throws the exception.

    public class CrmTracing : ITracingService
    {
        ITracingService _tracingService;
        StringBuilder _internalTrace;

        public CrmTracing(ITracingService tracingService)
        {
            _tracingService = tracingService;
            _internalTrace = new StringBuilder();
        }

        public void Trace(string format, params object[] args)
        {
            if (_tracingService != null) _tracingService.Trace(format, args);
            _internalTrace.AppendFormat(format, args).AppendLine();
        }

        public string GetTraceBuffer()
        {
            return _internalTrace.ToString();
        }
    }

Just instantiate it in your plugin passing in the CRM provided ITracingService. Since it is the same interface it works the same if you pass it to other classes and methods.

public class MyPlugin : IPlugin
{

    public void Execute(IServiceProvider serviceProvider)
    {
        var tracingService = new CrmTracing((ITracingService)serviceProvider.GetService(typeof(ITracingService)));

        tracingService.Trace("Works same as always.");

        var trace = tracingService.GetTraceBuffer();
    }
}



回答2:


To get the traceInfo string from traceService at runtime I used debugger to interrogate the tracingService contents.

So the trace string is accessible from these expressions...

for Plugins

((Microsoft.Crm.Extensibility.PipelineTracingService)(tracingService)).TraceInfo 

for CWA

((Microsoft.Crm.Workflow.WorkflowTracingService)(tracingService)).TraceInfo 

You can drill into the tracing service by debugging and extract the expression.

However, at design time neither of these expressions seem to be accessible from any of the standard CRM 2011 SDK dlls... so not sure if its possible as yet.



来源:https://stackoverflow.com/questions/28091438/crm-2011-itracingservice-getting-access-to-the-traceinfo-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!