Console's output not automatically redirected to Visual Studio's output window in debug mode

In the C#12 In a Nutshell book, it says:

When running WPF or Windows Forms applications under Visual Studio, the Console’s output is automatically redirected to Visual Studio’s output window (in debug mode)

However, I tried this in the code behind:

using System.Diagnostics;
using System.Windows;

namespace WPFHelloWorld
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Hello rr Debug");
            Console.WriteLine("Hello rr");
        }
    }
}

And I only see the output of Debug.WriteLine (so the code is reached).

I checked the Output tab and the Immediate Window tab.

I don't see the "Hello rr" in either of them. I see "Hello rr Debug" in the Output tab.

Why is this not working like the book says it should?

Is this a bug of Visual Studio? I know the good practice is to use Debug.WriteLine("Hello rr Debug"); in WPF apps since there is no Console. My question is why is the Console's output not redirected when it says it should.

Things I already did:

Upvotes: 1

Views: 38

Answers (2)

Ivan Petrov
Ivan Petrov

Reputation: 4915

It seems that it's a feature that is no longer offered in newer Visual Studio + modern .NET (Core) combinations.

Look at this thread

Personally, I can still get the behavior described in the book on VS 2019/2022 if I target .NET Framework 4.7.2+ and not .NET (Core).

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37460

Project setup

You could try setting output type of application to "Console Applicaiton" in project's properties: enter image description here

Confirm it's working

I defined sample window with constructor:

public MainWindow()
{
    Console.WriteLine("CONSOLE");
    Debug.WriteLine("DEBUG");

    InitializeComponent();
}

Then Console would log to console (that would start alongside WPF window), and debug to debug output:

enter image description here

Upvotes: 0

Related Questions