好的,我正在尝试使用尾来监视日志文件,但是我不能像使用相同的参数在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项目,并且将切换到它,因为缓冲区使这个解决方案太慢了。
谢谢你的回答。
发布于 2008-10-26 12:30:28
当重定向时,Process.StandardOutput默认为带有4096字节缓冲区的StreamReader,因此答案是肯定的。
发布于 2008-10-26 12:27:48
在大多数语言和操作系统中,标准流通常是缓冲的,但错误流不是。
尝试使用:System.Console.Error
https://stackoverflow.com/questions/237914
复制相似问题