How to prevent VS2022 Output window from swallowing Trace.Write() messages?

AMG 10 Reputation points
2023-05-31T18:41:56.8733333+00:00

I'm running a .NET 7.0 project from VS2022 and I'd like to simply output all Trace messages to DebugView++. Everything works great, but only when there is no debugger attached. If I start debugging the project in VS, Trace.Write() goes only to the Output window. If I detach the debugger, DebugView++ will immediately begin showing the Trace messages. If I reattach the debugger, it will immediately stop writing to DebugTrace and writes in the Output window. I can switch it all day long, so it seems the Output window is redirecting all Trace output to the Output window; all I want is for VS to stop screwing with the Trace output always and forever. Is this even possible?

Here's a simple logger class I'm using to test this.


    public static class Logger
    {
        readonly static TraceListener? listener = null;

        static Logging()
        {
            Trace.Listeners.Clear();

            listener = new DefaultTraceListener();
            Trace.Listeners.Add(listener);

            Trace.AutoFlush = true;
        }

        public static void Trace(string message, [CallerMemberName] string callerName = "")
        {
            var msg = $"[TestProject|{callerName}] {message}";

            System.Diagnostics.Trace.WriteLine(msg);
        }
    }

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,462 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 31,006 Reputation points Microsoft External Staff
    2023-06-01T09:05:17.3866667+00:00

    Hi @AMG, 

    Welcome to Microsoft Q&A! 

    I suppose it is designed like this.

    If you attach a debugger to the process, the output of Trace.Write() will go to the debugger, not DebugView. 

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.