Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 436
glenatron Avatar asked Nov 16 '25 01:11

glenatron


1 Answers

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.

like image 171
2 revsglenatron Avatar answered Nov 17 '25 14:11

2 revsglenatron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!