如何在Linux上以单声道(c#)运行的应用程序中重定向输出,以避免“数组索引超出范围”。当多个线程写入控制台?

时间:2012-03-02 06:59:45

标签: c# mono

在我们的应用程序中,我们以下列方式重定向输出:


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

1 个答案:

答案 0 :(得分:2)

StreamWriter 不是线程安全的。如果您希望输出提交不同的主题,则应使用 TextWriter.Synchronized