Can output from OutputDebugString be viewed in Visual Studio's output window?
Asked Answered
P

5

26

I am using C# and Visual Studio 2010.

When I use OutputDebugString to write debug information, should it show up in the output window?

I can see the output from OutputDebugString in DebugView, but I thought I would see it in Visual Studio's Output window. I have looked under menu Tools ? Options ? Debugging ? General, and the output is NOT being redirected to the Immediate window. I have also looked under menu Tools* ? Options ? Debugging ? Output Window and all General Output Settings are set to "On". Finally, I have used the drop-down list in the Output window to specify that Debug messages should appear.

If I change menu Tools* ? Options ? Debugging ? General to redirect the output to the Immediate window, the OutputDebugString messages do not appear in the immediate window.

Here is my entire test program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace OutputDebugString
{
  class Program
  {
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern void OutputDebugString(string message);

    static void Main(string[] args)
    {
      Console.WriteLine("Main - Enter - Console.WriteLine");
      Debug.WriteLine("Main - Enter - Debug.WriteLine");
      OutputDebugString("Main - Enter - OutputDebugString");
      OutputDebugString("Main - Exit - OutputDebugString");
      Debug.WriteLine("Main - Exit - Debug.WriteLine");
      Console.WriteLine("Main - Exit - Console.WriteLine");
    }
  }
}

If I run within the debugger, the Debug.WriteLine output does show up in the output window, but the OutputDebugString output does not.

If I run from a console window, both Debug.WriteLine and OutputDebugString show up in DebugView.

Why doesn't the OutputDebugString output ever show up in the output window?

Ultimately, my intent is not to write a lot of debug output with OutputDebugString, rather I will use System.Diagnostics or NLog or something similar. I am just trying to find out, if I configure a logging platform to write to OutputDebugString, will the output be visible from within the debugger.

I went back to my original program (not the simple test above) which uses TraceSources and TraceListeners configured via the app.config file. If I configure the trace sources to write to the System.Diagnostics.DefaultTraceListener (which is documented as writing to OutputDebugString), then the trace source output DOES go to the debug window. However, lines that write directly with OutputDebugString (such as in my simple example) DO NOT go to the debug window. Also, if I use a different TraceListener that writes to OutputDebugString (I got one from Ukadc.Diagnostics at Codeplex), that output DOES NOT go to the debug window.

One note about the Ukadc.Diagnostics trace listener... Ukadc.Diagnostics contains some trace listeners that allow for custom formatting of output (similar to the formatting that is available in log4net, NLog, and LAB). So, with "only" a dependency on Ukadc.Diagnostics one can use "standard" .NET diagnostic logging, but I can get some advanced features (like the output formatting) without becoming dependent on a possibly much larger platform. In this case, I could use the Ukadc.Diagnostics OutputDebugStringTraceListener to write logging output to the debug window in the same format (if desired, or a different format) as it would be if written to a file.

Note that I have seen these questions, but they did not provide a working solution:

Here and here

Penton answered 14/6, 2010 at 18:51 Comment(2)
To ask the silly question, why aren't you just using Debug.Write? (and firends)Asterisk
Ultimately I intend to use System.Diagnostics or some other logging platform. System.Diagnostics has the TraceSource/TraceListener architecture that allows for the logging destination (file, console, debug window, etc) to be changed by changing the app.config file. If I instrument the code with TraceSource.TraceEvent (or similar), then it would be nice to be able to see the output in the VS output window simply by changing the TraceListener to an "OutputDebugString" listener. Note that if I use the default trace listener, the output does go to the output window.Penton
C
39

You had me going on this question for a while. No way! Way.

Project > Properties > Debug tab, turn on the "Enable unmanaged code debugging" checkbox. Renamed to "Enable native code debugging" in later VS versions. With the unmanaged code debugging engine enabled, OutputDebugString() output is now properly intercepted and directed to the Output window.

Conners answered 14/6, 2010 at 19:43 Comment(3)
That was it exactly! I changed that setting in my sample program and then back in my original program and it fixed in both places. Accepting this answer because it is right!Penton
+1. We are using a native DLL that puts messages via OutputDebugString and were unable to see them.Synodic
This option isn't there for me, did they move it?Sextans
J
9

When debuggging (Debug => Start Debugging F5), the setting Project + Properties, Debug tab, check "Enable unmanaged code debugging" works nicely.

When NOT debugging (Debug => Start Without Debugging CTRL+F5) you mujst use DebugView from the SysInternals library. Download DebugView for Windows v4.76

Josephinajosephine answered 20/2, 2011 at 10:50 Comment(0)
G
4

It might be showing in the "Immediate Window" instead due to a setting:

  • Go to Tools/Options/Debugging/General. Uncheck "Redirect all Output Window text to the Immediate Window"

Or somethig like that.

Gwenora answered 14/6, 2010 at 18:56 Comment(1)
Thanks. I looked and that option is not checked. If I do check it, my output also does not appear in the Immediate window.Penton
P
1

I had Project > Properties > Debug tab > ticked the "Enable unmanaged code debugging" but OutputDebugString() didn't work even though breakpoints did.

I had also Solution > Common Properties > Project Dependencies > ticked my DLL under my C# project but build dependencies didn't work.

The fix for both these issues was to Solution > Configuration Properties > tick build on my DLL.

Plicate answered 28/7, 2020 at 19:47 Comment(0)
P
0

I had this problem once and all solutions suggested in this thread failed. DebugView was able to see debug messages, Visual Studio 2017 wasn't, no matter how I tried to configure it. Enable native code debugging? Check. No redirect to "immediate"? Check. Project is not excluded from compilation? Check.

Eventually I solved issue via Visual Studio Installer by using "Repair" option on my installation. Took about 10 minutes to complete.

Peerless answered 22/8, 2022 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.