在我们的应用程序中,我们以下列方式重定向输出:
FileStream fsOut = new FileStream(filename, FileMode.Append, FileAccess.Write);
swOut = new StreamWriter(fsOut);
Console.SetOut(swOut);
Console.SetError(swOut);
测试:
private StreamWriter _swOut = null;
[TestCase(100, "output.txt")]
public void RawThreadsTests(int howMany, string filename) {
FileStream fsOut = new FileStream(filename, FileMode.Append, FileAccess.Write);
_swOut = new StreamWriter(fsOut);
Console.SetOut(_swOut);
Console.SetError(_swOut);
Thread[] threads = new Thread[howMany];
Container c = TestContainer;
for(int i = 0; i < howMany; i++) {
threads[i] = new Thread(unused => Write());
threads[i].Start();
}
//Wait for all threads
for(int i = 0; i < howMany; i++) {
threads[i].Join();
}
}
static void Write() {
Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
顺便说一句。记录字符串的长度是一些NHibernate log sql语句的长度。
在mono 2.10.1上运行此测试将导致多个:
(..).RawThreadsTests(100,"output.txt") : System.IndexOutOfRangeException: Array index is out of range.
at System.IO.StreamWriter.LowLevelWrite (System.String s) [0x00000] in :0
at System.IO.StreamWriter.Write (System.String value) [0x00000] in :0
at System.IO.TextWriter.WriteLine (System.String value) [0x00000] in :0
at System.Console.WriteLine (System.String value) [0x00000] in :0
(..).RawThreadsTests(100,"output.txt") : System.ArgumentException: Reading would overrun buffer
at System.IO.FileStream.Write (System.Byte[] array, Int32 offset, Int32 count) [0x00000] in :0
at System.IO.StreamWriter.FlushBytes () [0x00000] in :0
at System.IO.StreamWriter.Decode () [0x00000] in :0
at System.IO.StreamWriter.LowLevelWrite (System.String s) [0x00000] in :0
at System.IO.StreamWriter.Write (System.String value) [0x00000] in :0
at System.IO.TextWriter.WriteLine (System.String value) [0x00000] in :0
at System.Console.WriteLine (System.String value) [0x00000] in :0
如果未重定向输出,则这些测试不会失败。
注意:与许多包含多线程的测试一样,并非每次运行都会产生错误。
是否有其他方法或将Console.WriteLine重定向到应用程序内部的文件?
先谢谢,Tymek