Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Writeline C# - Loses STDOUT when logging

Tags:

c#

stdout

console

I am in a small pinch :)

I found a very wierd behaviour and i cannot figure out what could be the cause of it.

It is related to console Logging in C# Visual studio 2008

For example, a normal behaviour of command line is :

1) If i use the "dir" command i see the contents of the directory

2) If i use "dir > 1.txt" i get the contents of the directory in the 1.txt file.

so this is the normal behaviour and it works. Now back to my problem.

In the c# application i am using, i have a list of tests and i want to list them when i use my application from command line. so i have:

Console.WriteLine("Start running " + sn);

The outcome when i run the application normally is: (The command i send)

Application.exe -run 1.test,2.test,3.test

Output:

Start running 1.test
Start running 2.test
Start running 3.test

but if i use this command:

Application.exe -run 1.test,2.test,3.test > 1.txt

The three Start running commands disappear from the stdout but the 1.txt is BLANK.

I have no idea where the response goes, i have no way of logging the response, but i can see it visually. But when i redirect STDOUT to a log neither it exists visually nor in the log.

Anyone have any idea?

like image 791
MaTThZero Avatar asked May 28 '13 11:05

MaTThZero


People also ask

What is console WriteLine in C?

WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

What is console WriteLine in programming?

While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line. Program 1: Example of Console.

What is difference between write () and WriteLine ()?

The difference between Write() and WriteLine() method is based on new line character. Write() method displays the output but do not provide a new line character. WriteLine() method displays the output and also provides a new line character it the end of the string, This would set a new line for the next output.

Where does console WriteLine go C#?

It goes to the console (standard output) or to the stream that the console is set to.


1 Answers

The output to file is buffered. In the worst case the content will be saved after the application finishes. To force buffer flush, you may call:

Console.Out.Flush();
like image 182
lisp Avatar answered Sep 20 '22 13:09

lisp