1
使用c#,是否有將控制檯輸出複製到第二個位置(以及原始控制檯)的方法。我知道我可以調用SetOut來覆蓋控制檯的默認輸出位置,但我想要做的是繼續寫入原始控制檯實現,但也寫入第二個位置。有任何想法嗎?c#將控制檯輸出複製到第二個位置
使用c#,是否有將控制檯輸出複製到第二個位置(以及原始控制檯)的方法。我知道我可以調用SetOut來覆蓋控制檯的默認輸出位置,但我想要做的是繼續寫入原始控制檯實現,但也寫入第二個位置。有任何想法嗎?c#將控制檯輸出複製到第二個位置
你可以用這個代碼
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine ("Cannot open Redirect.txt for writing");
Console.WriteLine (e.Message);
return;
}
Console.SetOut (writer);
Console.WriteLine ("This is a line of text");
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
我建議你看看[log4net的(http://logging.apache.org/log4net/)試試。 – Polynomial 2012-07-24 09:28:42
看看這個答案在這裏:http://stackoverflow.com/a/420502/82447 – klennepette 2012-07-24 09:29:53