sieben
sieben

Reputation: 2201

RedirectStandardOutput is buffering lines instead of being instantaneous?

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.

Upvotes: 0

Views: 2290

Answers (2)

Alan
Alan

Reputation: 6679

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

Upvotes: 3

tovare
tovare

Reputation: 4087

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

Try using: System.Console.Error

Upvotes: 1

Related Questions