Streamwriter 写的与 Console.WriteLine() 不同
Streamwriter writes different then Console.WriteLine()
我有一个 streamwriter 将进程流中的数据写入日志文件。当我用 Console.WriteLine()
显示输出时,结果如下所示:
SW DATA = Sep 23, 2016 9:40:37 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Trying protocol: JNLP3-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Server didn't accept the handshake: Unknown protocol:Protocol:JNLP3-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Connecting to Machine:51136
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Trying protocol: JNLP2-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Connected
但是当我打开文本文件时,它看起来像:
INFO: Connected
:38 AM hudson.remoting.jnlp.Main$CuiListener status
LP3-connect
6231dd with ID
ad is recommended.
我的密码是:
static void ShowOutput(string data)
{
if (data != null)
{
FileStream fileStream = new FileStream(@"c:\temp\LogFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
Console.WriteLine("SW DATA = " + data);
using (StreamWriter w = new StreamWriter(fileStream))
{
w.WriteLine(data);
w.Flush();
}
fileStream.Close();
}
}
看来作者只写了每一行的结尾。
数据来自与此类似的控制台进程:
http://www.codeproject.com/Articles/4665/Launching-a-process-and-displaying-its-standard-ou
我的问题是:
我怎样才能避免这种情况?
It looks like the writer only writes the end of every Line.
这是因为您每次都重新打开文件,覆盖了之前的内容。您需要在调用 ShowOutput
之间保持文件打开,或者以 Append
模式打开它。
文件中的混淆内容是因为它只覆盖了文件的开头(并没有截断它),所以如果你写的日志条目比前面的短,那么前面的结尾文本仍然存在。
我有一个 streamwriter 将进程流中的数据写入日志文件。当我用 Console.WriteLine()
显示输出时,结果如下所示:
SW DATA = Sep 23, 2016 9:40:37 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Trying protocol: JNLP3-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Server didn't accept the handshake: Unknown protocol:Protocol:JNLP3-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Connecting to Machine:51136
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Trying protocol: JNLP2-connect
SW DATA = Sep 23, 2016 9:40:38 AM hudson.remoting.jnlp.Main$CuiListener status
SW DATA = INFO: Connected
但是当我打开文本文件时,它看起来像:
INFO: Connected
:38 AM hudson.remoting.jnlp.Main$CuiListener status
LP3-connect
6231dd with ID
ad is recommended.
我的密码是:
static void ShowOutput(string data)
{
if (data != null)
{
FileStream fileStream = new FileStream(@"c:\temp\LogFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
Console.WriteLine("SW DATA = " + data);
using (StreamWriter w = new StreamWriter(fileStream))
{
w.WriteLine(data);
w.Flush();
}
fileStream.Close();
}
}
看来作者只写了每一行的结尾。
数据来自与此类似的控制台进程:
http://www.codeproject.com/Articles/4665/Launching-a-process-and-displaying-its-standard-ou
我的问题是:
我怎样才能避免这种情况?
It looks like the writer only writes the end of every Line.
这是因为您每次都重新打开文件,覆盖了之前的内容。您需要在调用 ShowOutput
之间保持文件打开,或者以 Append
模式打开它。
文件中的混淆内容是因为它只覆盖了文件的开头(并没有截断它),所以如果你写的日志条目比前面的短,那么前面的结尾文本仍然存在。