Axis
Axis

Reputation: 846

Reading the System.Diagnostics.Debug window

I have a Netduino and it is currently outputting accelerometer data to the System. Diagnostics.Debug window. I am waiting on a USB->232 converter in the mail to properly get the data from the device into my app via a serial port but I was wondering just for quick testing purposes if anyone knows if it is possible to read the data from the Debug window back into my app?

EDIT - Solution: I am posting this here for anyone who want my solution. I originally thought that Nuf's answer worked "Data written to System.Diagnostics.Debug can be captured with TraceListener class. MSDN has short tutorial how to set it up." but I found out that the Listener can only get data from within it's own application. Since I was using a Netduino, the Debug output was from a different program which meant the trace listener could not read it. So I found a way to read the text in the Output box directly.

Base on code from MSDN: You will need to 3 references to your project. They are located in the .Net reference tab - EnvDTE, EnvDTE80, and extensibility.

using EnvDTE80;
using EnvDTE;
using Extensibility;

        public static string ReadDebugBox()
        {
            EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
            string data = "";
            OutputWindow ow = dte.ToolWindows.OutputWindow;
            OutputWindowPane owP;

            TextDocument owPTxtDoc;
            EditPoint2 strtPt;

            owP = ow.OutputWindowPanes.Item("Debug");
            owP.Activate();
            owPTxtDoc = owP.TextDocument;

            strtPt = (EditPoint2)owPTxtDoc.StartPoint.CreateEditPoint();
            return strtPt.GetText(owPTxtDoc.EndPoint);
        }



            public static void ClearDebugBox()
            {
                EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
                OutputWindow ow = dte.ToolWindows.OutputWindow;
                OutputWindowPane owP;

                TextDocument owPTxtDoc;
                EditPoint2 strtPt;

                owP = ow.OutputWindowPanes.Item("Debug");
                owP.Activate();

                owP.Clear();
            }

There may be better ways of doing it but this is just one that worked for me so i thought I would share it.

Upvotes: 2

Views: 2273

Answers (2)

David Z.
David Z.

Reputation: 5701

There are two ways to set this up, one way is to declaratively set this up in your app.config file. There's a lot of advantages to doing this such as not needing to recompile your application when changes are needed.

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="configConsoleListener" 
          type="System.Diagnostics.ConsoleTraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Alternatively, you can also do this inside of your code to output Debug traces to your application's console window.

Debug.Listeners.Add(new ConsoleTraceListener());

Upvotes: 1

Ňuf
Ňuf

Reputation: 6217

Data written to System.Diagnostics.Debug can be captured with TraceListener class. MSDN has short tutorial how to set it up.

Upvotes: 1

Related Questions