views:

799

answers:

2

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.

+1  A: 

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

Try using: System.Console.Error

tovare
+1  A: 

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

Alan
There's no way to manually flush it?
sieben
Never needed it, but you can access the underlying stream using the BaseStream property, and thus theoretically you can Flush() that.
Alan
i tried that, but it didn't seem to do anything.
sieben