RedirectStandardOutput是缓冲线而不是瞬时?

时间:2023-01-17 22:24:55

Ok, I am trying to use Tail to monitor a log file, but I cannot get the same behavior programatically as when I manually run it through cmd prompt using the same parameters.

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

When run through cmd prompt it displays the new lines instantly. Programatically though, I have to wait for about 75+ new lines in log file before the 'buffer' unleashes all the lines.

在cmd提示符下运行时,它会立即显示新行。但是以编程方式,我必须在“缓冲区”释放所有行之前等待日志文件中大约75个以上的新行。

Here's the code I have now.

这是我现在的代码。

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);
}

I have used the OutputDataReceived event before but never had these buffering/spamming problems.

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

I am so confused with about right now.

我现在很困惑。

* Edit *

I found this wintail project on CodeProject and am going to be switching to that because the buffer makes this solution way too slow.

我在CodeProject上找到了这个wintail项目,并且我将要切换到那个,因为缓冲区使得这个解决方案太慢了。

Thanks for the answers.

谢谢你的回答。

2 个解决方案

#1


2  

Process.StandardOutput, when redirected, defaults to a StreamReader with a 4096-byte buffer, so the answer is yes.

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

#2


1  

In most languages and operating systems the standard stream is usually buffered, but the error stream is not.

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

Try using: System.Console.Error

尝试使用:System.Console.Error

#1


2  

Process.StandardOutput, when redirected, defaults to a StreamReader with a 4096-byte buffer, so the answer is yes.

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

#2


1  

In most languages and operating systems the standard stream is usually buffered, but the error stream is not.

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

Try using: System.Console.Error

尝试使用:System.Console.Error