views:

1093

answers:

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?

+1  A: 

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

Arnout
A: 

Thank you, it is using System.Diagnostics for tracing. Can a tracelistener be attached to the listeners for an existing process or does it need to be configured in? My guess is that it can be but only through Reflection?

glenatron
The TraceListener to be used is normally defined in the app.config / web.config file. You could implement your trace listener that forwards all the stuff into a socket or a pipe and your external tool could listen to that.
DrJokepu
Oh yeah, I forgot the important part: You can add trace listeners to your existing process with System.Debug.Listeners. Unfortunately, this will not work from an other process, so you will need to use Process.ErrorDataReceived and output your trace data into the error stream.
DrJokepu
+1  A: 

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