MaTThZero
MaTThZero

Reputation: 79

Console.Writeline C# - Loses STDOUT when logging

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?

Upvotes: 7

Views: 9925

Answers (1)

lisp
lisp

Reputation: 4198

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();

Upvotes: 19

Related Questions