127

I have put some Console.WriteLine calls in to test, but they aren't appearing in the output box?

public static ArrayList myDeliveries = new ArrayList();

public mainForm(){
    InitializeComponent();
}

private void mainForm_Load(object sender, EventArgs e){

    if (!File.Exists("../../MealDeliveries.txt")){
        MessageBox.Show("File not found!");
        return;
    }

    using (StreamReader sr = new StreamReader("../../MealDeliveries.txt")){
        //first line is delivery name 
        string strDeliveryName = sr.ReadLine();
        Console.WriteLine("Test content");

        while (strDeliveryName != null){

            //other lines 
            Delivery d = new Delivery(
                strDeliveryName, 
                sr.ReadLine(),
                sr.ReadLine(), 
                sr.ReadLine(),
                sr.ReadLine(), 
                sr.ReadLine(),
                sr.ReadLine()
            );

            mainForm.myDeliveries.Add(d);

            //check for further values
            strDeliveryName = sr.ReadLine();
        }
    }

    displayDeliveries();


}


private void displayDeliveries(){

    lstDeliveryDetails.Items.Clear();
    Console.WriteLine("Test content");
    Console.WriteLine(mainForm.myDeliveries.Count);
    foreach (Delivery d in mainForm.myDeliveries){
        lstDeliveryDetails.Items.Add(d.DeliveryName);

    }
}

Can anyone help??

sark9012
  • 5,485
  • 18
  • 61
  • 99

9 Answers9

173

Console outputs to the console window and Winforms applications do not show the console window. You should be able to use System.Diagnostics.Debug.WriteLine to send output to the output window in your IDE.

Edit: In regards to the problem, have you verified your mainForm_Load is actually being called? You could place a breakpoint at the beginning of mainForm_Load to see. If it is not being called, I suspect that mainForm_Load is not hooked up to the Load event.

Also, it is more efficient and generally better to override On{EventName} instead of subscribing to {EventName} from within derived classes (in your case overriding OnLoad instead of Load).

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
  • 7
    Like this? System.Diagnostics.Debug.WriteLine("some tetttttttttt23423423423423423ttttttttttttttttttttttt"); – sark9012 Apr 19 '10 at 17:40
  • 3
    @Luke - yes, but you can `Import System.Diagnostics` to make it much simpler. And I prefer using the `Trace` class for this. – Joel Coehoorn Apr 19 '10 at 17:46
  • 3
    In the top navbar of Visual Studio go to View > Output to see the values from `System.Diagnostics.Debug.WriteLine`. – 2Yootz Dec 11 '17 at 22:22
67

If you intend to use this output in production, then use the Trace class members. This makes the code portable, you can wire up different types of listeners and output to the console window, debug window, log file, or whatever else you like.

If this is just some temporary debugging code that you're using to verify that certain code is being executed or has the correct values, then use the Debug class as Zach suggests.

If you absolutely must use the console, then you can attach a console in the program's Main method.

Community
  • 1
  • 1
Aaronaught
  • 120,909
  • 25
  • 266
  • 342
16

If you want Console.WriteLine("example text") output to show up in the Debug Output window, temporarily change the Output type of your Application from Console Application to Windows Application.

From menus choose Project + Properties, and navigate to Output type: drop down, change to Windows Application then run your application

Of course you should change it back for building a console application intended to run outside of the IDE.

(tested with Visual Studio 2008 and 2010, expect it should work in latter versions too)

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
davervw
  • 171
  • 1
  • 5
14

Using Console.WriteLine( "Test" ); is able to write log messages to the Output Window (View Menu --> Output) in Visual Studio for a Windows Forms/WPF project.

However, I encountered a case where it was not working and only System.Diagnostics.Debug.WriteLine( "Test" ); was working. I restarted Visual Studio and Console.WriteLine() started working again. Seems to be a Visual Studio bug.

Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
Dhiren Sarup
  • 141
  • 1
  • 2
  • 1
    And also; building stuff in VS switches the output window to build; so now I have to click output, change item in 'show output from' AND scroll through tens of other messages. – Christian Dec 17 '20 at 10:47
  • 1
    As of today, in VS 2019 ( Ver. 16.11.19 ) this is the correct answer! Thanks. – BHP Jan 05 '23 at 06:11
3

If you are developing a command line application, you can also use Console.ReadLine() at the end of your code to wait for the 'Enter' keypress before closing the console window so that you can read your output. However, both the Trace and Debug answers posted above are better options.

paul jerman
  • 601
  • 5
  • 13
2

Try to uncheck the CheckBox “Use Managed Compatibility Mode” in

Tools => Options => Debugging => General

It worked for me.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • I had this on when debugging a previous cpp/cli project and apparently causes this issue as well as NLog not logging to console. – Blackey Jan 14 '20 at 02:38
2

Try to uncheck the CheckBox “Redirect all Output Window text to the Immediate Window” in

Tools => Options => Debugging => General

I had it checked and everything was written in the Immediate Window and not in the Output Window

Oscar Hermosilla
  • 480
  • 5
  • 21
1

When issue happening on Mac VS 2017 (Which I faced).

  1. Go to Project >> "Your Project name" options.
  2. An option window will pop up
  3. Go to RUN >> Default menu option
  4. Tick the "Run on external console" option TRUE and say OK

Run your application code now.

VSB
  • 317
  • 2
  • 10
-2

Old Thread, But in VS 2015 Console.WriteLine does not Write to Output Window If "Enable the Visual Studio Hosting Process" does not Checked or its Disabled in Project Properties -> Debug tab

Blak RUSH
  • 9
  • 1
  • 5