简体   繁体   English

Console.Writeline C# - 记录时丢失STDOUT

[英]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 它与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 1)如果我使用“dir”命令,我会看到目录的内容

2) If i use "dir > 1.txt" i get the contents of the directory in the 1.txt file. 2)如果我使用“dir> 1.txt”,我将获得1.txt文件中目录的内容。

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. 在我正在使用的c#应用程序中,我有一个测试列表,我想在命令行中使用我的应用程序时列出它们。 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. 三个Start start命令从stdout中消失,但1.txt是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. 但是当我将STDOUT重定向到日志时,它既不存在于视觉上也不存在于日志中。

Anyone have any idea? 任何人都有任何想法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM