CRM 2011 - ITracingService在运行时访问traceInfo [英] CRM 2011 - ITracingService getting access to the traceInfo at runtime

查看:320
本文介绍了CRM 2011 - ITracingService在运行时访问traceInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的插件中有一些自定义日志记录,并希望在我的自定义日志记录中包含我的tracingService的内容(在插件完成之前在catch块中调用)。



我似乎无法访问tracingService的内容。我想知道它是否可以访问?



我尝试过tracingService.ToString()只是让开发人员提供了一个有用的重载,唉像预期的,我得到类的名称Microsoft.Crm.Sandbox.SandboxTracingService。



显然,如果需要,Dynamics CRM会将tracingService内容用于管道的末尾。



有人有任何想法吗?



Kind Regards,
Gary

解决方案

跟踪服务在执行期间不提供对跟踪文本的访问,但可以通过创建自己的 ITracingService 。请注意,在您调用插件的 Execute 方法之前,您无法获取写入跟踪日志的任何文本 - 这意味着如果您有多个插件被激活,您将无法获取他们在插件中的跟踪输出引发异常。

  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();
}
}

只需在您的插件中实例化即可提供CRM ITracingService 。由于它是相同的界面,如果将其传递给其他类和方法,它的工作方式相同。

  public class MyPlugin:IPlugin 
{

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

tracingService.Trace(与以前一样工作);

var trace = tracingService.GetTraceBuffer();
}
}


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

解决方案

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();
    }
}

这篇关于CRM 2011 - ITracingService在运行时访问traceInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆