RedirectStandardOutput 是缓冲行而不是瞬时的?

发布于 2024-07-08 02:46:05 字数 1290 浏览 10 评论 0原文

好的,我正在尝试使用 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 事件,但从未遇到过这些缓冲/垃圾邮件问题。

我现在很困惑。

* 编辑 *

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

感谢您的回答。

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.

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.

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.

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.

Thanks for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-07-15 02:46:05

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

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

幸福还没到 2024-07-15 02:46:05

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

尝试使用:
<代码>
系统控制台错误

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

Try using:

System.Console.Error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文