addin插件Trace日志的使用

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)


在开发addin插件的时候,为addin插件的日志加上来是一件很方便后续问题定位的事情,系统的消息会自动添加到日志中,添加方法很简单。

// 调试日志信息打印

System.Diagnostics.TextWriterTraceListener _pDebugerTraceListener = null;

 

// 初始化调试信息Listener

if (_pDebugerTraceListener == null)

{

    // 如果不存在就创建c:/log文件夹

    if (Directory.Exists(@"c:/log/") == false)

    {

        Directory.CreateDirectory(@"c:/log/");

    }

    // 指定文件名

    int nPid = System.Diagnostics.Process.GetCurrentProcess().Id;

    String sLogFileName = "AddinTrace_" + DateTime.Today.ToShortDateString() + "_pid" + nPid + ".txt";

   

    // 初始化Listener

    _pDebugerTraceListener = new System.Diagnostics.TextWriterTraceListener(@"c:/log/" + sLogFileName);

   

    // 把Listener添加到监听列表中

    System.Diagnostics.Debug.Listeners.Add(_pDebugerTraceListener);

}

 

 

// 删除关闭Listener方法

if (_pTraceListener != null)

{

    System.Diagnostics.Debug.Listeners.Remove(_pDebugerTraceListener);

    _pDebugerTraceListener.Close();

}

 

 

之后,就能监听到Debug和addin插件通信的各种消息了:

像比较重要的消息

IDebugEnterBreakStateEvent   进入断点状态事件

IDebugExitBreakStateEvent    退出断点状态事件

IDebugExpressionsDirtyEvent   执行命令或GetExpression等操作

IDebugOutputStringEvent2     outputdebugstring事件

[PDTDebug] received IDebugProgramCreateEvent2   // 程序创建

[PDTDebug] received IDebugProgramCreateEvent2

[PDTDebug] received IDebugBreakpointErrorEvent2  // 断点

[PDTDebug] received IDebugBreakpointErrorEvent2

[PDTDebug] received IDebugModuleLoadEvent2     // 模块加载

[PDTDebug] received IDebugThreadCreateEvent2    // 线程创建

[PDTDebug] received IDebugModuleLoadEvent2

[PDTDebug] received IDebugModuleLoadEvent2

[PDTDebug] received IDebugModuleLoadEvent2

[PDTDebug] received IDebugLoadCompleteEvent2    // 加载结束

[PDTDebug] received IDebugOutputStringEvent2     // outputdebugstring事件

[PDTDebug] received IDebugOutputStringEvent2   

[PDTDebug] received IDebugEntryPointEvent2       //

[PDTDebug] received IDebugThreadCreateEvent2     // 线程创建

[PDTDebug] received IDebugThreadDestroyEvent2    // 线程销毁

[PDTDebug] received IDebugBreakpointBoundEvent2  // 断点绑定

[PDTDebug] received IDebugBreakpointBoundEvent2

[PDTDebug] received IDebugBreakpointBoundEvent2

[PDTDebug] received IDebugBreakpointErrorEvent2  

[PDTDebug] received IDebugBreakpointEvent2       // 断点事件

[PDTDebug] received IDebugEnterBreakStateEvent    // 进入断点状态事件

[PDTDebug] received IDebugCurrentThreadChangedEvent100

[PDTDebug] received IDebugExpressionsDirtyEvent    // 执行命令或GetExpression等操作

[PDTDebug] received IDebugOutputStringEvent2      // outputdebugstring事件

[PDTDebug] received IDebugBreakpointBoundEvent2  // 去除断点事件

[PDTDebug] received IDebugExitBreakStateEvent      // 退出断点状态事件

[PDTDebug] received IDebugCurrentThreadChangedEvent100

[PDTDebug] received IDebugExpressionsDirtyEvent    // 执行命令或GetExpression等操作

[PDTDebug] received IDebugProcessContinueEvent100  // 执行继续事件

 

也可以打印自定义的输出信息,形如

// 打印自定义信息

System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + ":" + sLogInfo + "\n");

if (_pTraceListener != null)

    _pTraceListener.Flush();

 

 

特别注意一个问题:

在VS2013的addin插件调试插件开发的时候,插件中调用GetExpression的嵌入函数不建议里面有:outputdebugString操作。改操作有时会阻塞,导致嵌入调用阻塞,从而引起GetExpression超时。

 

例如:在addin的代码中,通过GetExpression方法调用Testd.dll中的getSummaryInfo导出函数,如果里面有getSummaryInfo中有outputdebugString的话,可能会导致GetExpression调用超时,从而引发异常:

{,, Testd.dll}getSummaryInfo(0x111111)

 

GetExpression用法:

debugger.GetExpression(strExp, false, timeOutSecond);

GetExpression第二个参数为true时,会展开返回值到value中,并以 ""括号括起来;但是展开的值是阶段处理后的,当值比较长时,返回的不全。



 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)


你可能感兴趣的:(addin插件Trace日志的使用)