3

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?

glenatron
  • 11,018
  • 13
  • 64
  • 112

2 Answers2

5

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.

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • What do you do in HandleLogMessage? it tells me i cannot access internal delegate LogMessageEventHandler? – Apqu Jan 25 '15 at 15:39
  • @Tur, I'm afraid this was a few years and a couple of versions of .Net ago - I couldn't say at all that they haven't changed the behaviour of this somewhat. – glenatron Jan 25 '15 at 22:27
1

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

Arnout
  • 2,780
  • 14
  • 12