Console.WriteLine does not show up in Output window

C#.NetWinforms

C# Problem Overview


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??

C# Solutions


Solution 1 - C#

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).

Solution 2 - C#

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.

Solution 3 - C#

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)

Solution 4 - C#

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.

Solution 5 - C#

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

> Tools => Options => Debugging => General

It worked for me.

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsark9012View Question on Stackoverflow
Solution 1 - C#Zach JohnsonView Answer on Stackoverflow
Solution 2 - C#AaronaughtView Answer on Stackoverflow
Solution 3 - C#davervwView Answer on Stackoverflow
Solution 4 - C#Dhiren SarupView Answer on Stackoverflow
Solution 5 - C#slava.stackoverflowView Answer on Stackoverflow
Solution 6 - C#paul jermanView Answer on Stackoverflow
Solution 7 - C#VSBView Answer on Stackoverflow
Solution 8 - C#Blak RUSHView Answer on Stackoverflow