How to intercept debugging information ( Debugview style ) in C#?

☆樱花仙子☆ 提交于 2019-12-31 02:14:11

问题


For testing purposes I'm planning to put together a little app that will listen for a particular event coming from an application and interact with it at that point.

Given that we're at a point in the testing process where changing the application code is out of the question, the ideal from my point of view would be to listen to the debugging trace from the application, a little like debugview does, and respond to that.

Can anyone offer guidance on how best to go about this?


回答1:


The way I found to do it used the Mdbg tools from Microsoft to give me access from the runtime to the core debugging information. The basic shape of the code I'm using looks like this:

 MDbgEngine mg;
 MDbgProcess mgProcess;
 try
 {
       mg = new MDbgEngine();
       mgProcess = mg.Attach(debugProcess.Id);
 }
 catch (Exception ed)
 {
       Console.WriteLine("Exception attaching to process " + debugProcess.Id );
       throw (ed);
 }
 mgProcess.CorProcess.EnableLogMessages(true);
 mgProcess.CorProcess.OnLogMessage += new LogMessageEventHandler(HandleLogMessage);
 mg.Options.StopOnLogMessage = true;
 mgProcess.Go().WaitOne();
 bool running = true;
 Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
  while (running)
   {
       try
       {
           running =mgProcess.IsAlive;
           mgProcess.Go().WaitOne();
        }
        catch
         {
            running = false;
         }
     }

It seems to work well enough for what I need at any rate, perhaps it will provide a useful template to anyone else who finds themselves in the same boat.




回答2:


Is the application that you want to trace using standard System.Diagnostics-based tracing? In that case you could build your own TraceListener.



来源:https://stackoverflow.com/questions/367966/how-to-intercept-debugging-information-debugview-style-in-c

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