Where Does System.Diagnostics.Debug.Write Output Appear

Where does System.Diagnostics.Debug.Write output appear?

As others have pointed out, listeners have to be registered in order to read these streams. Also note that Debug.Write will only function if the DEBUG build flag is set, while Trace.Write will only function if the TRACE build flag is set.

Setting the DEBUG and/or TRACE flags is easily done in the project properties in Visual Studio or by supplying the following arguments to csc.exe

/define:DEBUG;TRACE

Debug.WriteLine shows nothing

There are two likely causes for this behavior

  • The application is being compiled in Release mode and the Debug.WriteLine call is not in the final program
  • There is no trace listener in the program and hence nothnig to output the message

The easiest way to diagnose this is to change the code to

#if DEBUG
Console.WriteLine("the message");
#endif

If it prints then you have an issue with the trace listeners, else you're compiling in Release

OutputDebugString / System.Diagnostics.Debug.WriteLine and Debugger.IsAttached

If you are willing to get your hands dirty and use some native stuff you can achieve this. You'll need to use P/Invoke.

OutputDebugString, as you can read here, is based on 4 kernel objects. Mutex named DBWinMutex, a shared memory DBWIN_BUFFER and two events (DBWIN_BUFFER_READY and DBWIN_DATA_READY) as they wrote in the article - we can't relay on the mutex as it exists all the time. But we could check if the shared section is created.

If we import OpenFileMapping to our project from pinvoke.net

static class NativeFunctions
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr OpenFileMapping(
uint dwDesiredAccess,
bool bInheritHandle,
string lpName);
}

Then we can check if the section is created or not and based on that decide whether something is listening.

Writing this simple program

public const int FILE_MAP_READ = 0x0004;
static void Main(string[] args)
{
if (NativeFunctions.OpenFileMapping(FILE_MAP_READ, false, "DBWIN_BUFFER") != IntPtr.Zero)
{
Log("Someone is listening");
}
else
{
Log("I am here alone");
}
}

private static void Log(string log)
{
Debug.WriteLine(log);
Console.WriteLine(log);
}

When running without DebugView we get "I'm here alone"

Sample Image

and with the tool

Sample Image

Additionally to clarify the things ".. while System.Diagnostics.Debug.WriteLine is writing to internal trace listeners." Debug.WriteLine will also write to the same place as OutputDebugString but only if VS is not attached - otherwise VS will capture the log and post to its Output window.

Why does System.Diagnostics.Debug.WriteLine not work in Visual Studio 2010 C#?

Check following items -

  1. DEBUG mode is selected while debugging
  2. Debug option is selected in Output window -
    Sample Image
  3. See if breakpoint is hitting Debug.WriteLine in code
  4. Insert Debug.AutoFlush = true at the beginning of code
  5. Try checking if Platform for the solution is set to Any CPU and not x86 (or x64).
  6. Goto Project Properties--> Web - In the Debugger section, check the the ASP.NET option

Reference for Point #5 (Read the comment, It worked for that guy)

Where does .NET's Debug.Write() output to?

It writes to the default trace listener, which you would need to turn on:

Debug.Write Method

If you want this directed to the console you would need to add an instance of the ConsoleTraceListener:

In your .config file ensure you have the following entries:

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

You may also need to ensure that you've included the /d:TRACE flag when compiling your project to enable the output.

Writing to output window of Visual Studio

Add the System.Diagnostics namespace, and then you can use Debug.WriteLine() to quickly print a message to the output window of the IDE. For more details, please refer to these:

  • How to trace and debug in Visual C#
  • A Treatise on Using Debug and Trace classes, including Exception Handling

Debug.WriteLine() in C# - What does it do?

It will show the message/text in your output window at the bottom of Visual Studio, you can log with it some actions like "Constructor just started" then it is easier to find where error appeared. Another thing is that you can add variables to your debug output like:

Debug.WriteLine("Debug message:Shop.Add.Product({0})", Product);

Check it here: Debug.WriteLine Method



Related Topics



Leave a reply



Submit