代码之家  ›  专栏  ›  技术社区  ›  sieben

RedirectStandardOutput是缓冲行而不是瞬时输出?

  •  0
  • sieben  · 技术社区  · 16 年前

    好的,我正在尝试使用Tail来监视日志文件,但我无法通过编程获得与使用相同参数通过cmd提示符手动运行它时相同的行为。

    当通过cmd提示符运行时,它会显示新行 立即 .不过从程序上讲,我得等一下 75+条新线路 在日志文件中,在“缓冲区”释放所有行之前。

    这是我现在的代码。

    private const string tailExecutable = @"C:\tail.exe";
    private const string logFile = @"C:\test.log";
    
    private static void ReadStdOut()
    {
        var psi = new ProcessStartInfo
        {
            FileName = tailExecutable,
            Arguments = String.Format("-f \"{0}\"", logFile),
            UseShellExecute = false,
            RedirectStandardOutput = true
        };
    
        // Running same exe -args through cmd.exe 
        // works perfectly, but not programmatically.
        Console.WriteLine("{0} {1}", psi.FileName, psi.Arguments);
    
        var tail = new Process();
        tail.StartInfo = psi;
        tail.OutputDataReceived += tail_OutputDataReceived;
        tail.Start();
        tail.BeginOutputReadLine();
    }
    
    static void tail_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine(e.Data);
    }
    

    我以前使用过OutputDataReceived事件,但从未遇到过这些缓冲/垃圾邮件问题。

    我现在很困惑。

    * 编辑 *

    我发现了 this wintail project on CodeProject 我将切换到那个,因为缓冲区使这个解决方案太慢了。

    谢谢你的回答。

    2 回复  |  直到 5 年前
        1
  •  2
  •   Alan    16 年前

    过程。当重定向时,StandardOutput默认为具有4096字节缓冲区的StreamReader,因此答案是肯定的。

        2
  •  1
  •   tovare    16 年前

    在大多数语言和操作系统中,标准流通常是缓冲的,但错误流不是。

    尝试使用: System.Console.Error

    推荐文章