redirecting both standard output and standard error

Answered redirecting both standard output and standard error

  • 2012년 4월 10일 화요일 오후 10:14
     
      코드 있음

    Hi, 

    How to catch both standard output and standard error redirections? 

    From 

    http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.71).aspx

    it says, 

    if you redirect both standard output and standard error and then try to read both, for example using the following C# code.

    [C#] 
    string output = p.StandardOutput.ReadToEnd(); 
    string error = p.StandardError.ReadToEnd(); 
    p.WaitForExit();

    ... it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.

    Or, I should use asynchronous read operations on at least one of the streams.

    However, I still couldn't figure out how to catch both the standard output and standard error. 

    Please help, I need a 

    string output_with_error = ...

    How can I do that? 

    Thanks


모든 응답

  • 2012년 4월 10일 화요일 오후 10:57
    중재자
     
     

    Argh.  Dusting off the cob webs here.  The reason why it doesn't write the output until the process closes is because it doesn't close the stream, which flushes the buffers until the process closes.  I'm doing a search for some code that works around this, with some limitations, by forcing the stream to close before the process closes. 

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • 2012년 4월 11일 수요일 오후 12:56
     
     
    Thanks Rudy. Looking forward to it.
  • 2012년 4월 12일 목요일 오후 9:57
    중재자
     
     

    I have not been able to locate the old thread migrated from the old forums server.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

  • 2012년 4월 13일 금요일 오후 1:20
     
     

    NP. Thank you all the same for your efforts. 

    I'll continue searching...
    • 편집됨 smetah 2012년 4월 13일 금요일 오후 1:20
    •  
  • 2012년 4월 13일 금요일 오후 1:59
     
     
    Process has events for "OutputDataReceived" and "ErrorDataReceived"  There's an example of using asynchronous reading of output here.  Doing the same for the error list is going to look pretty much exactly the same with just slightly different property/method names.
  • 2012년 4월 13일 금요일 오후 2:06
     
     
    Thanks!

    Alright,

    When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.

    In that case, can I use the same event handler for both StandardOutput and StandardError stream? 
    Will that create a race condition? 

    Thanks
  • 2012년 4월 13일 금요일 오후 2:52
     
     

    "In that case, can I use the same event handler for both StandardOutput and StandardError stream? "

    You can the question is whether you want to or not.

    "Will that create a race condition?"

    That depends on what you do inside of the method.  Could you make a race condition, yes.  Could you prevent race conditions, also yes.

    If you show us what you're doing here we could help with that, but just assume that the handler could be called concurrently.
  • 2012년 4월 13일 금요일 오후 2:54
     
     

    All I wanted is to catch both StandardOutput and StandardError into the same string. 

    How can I do that? 

    thanks

  • 2012년 4월 13일 금요일 오후 3:01
     
     
    You should make two separate strings as stream destinations and then concatenate them together once both are read.  The process might try to write to both streams at the same time, so you should be prepared for this situation.
  • 2012년 4월 13일 금요일 오후 3:18
     
     답변됨

    All I wanted is to catch both StandardOutput and StandardError into the same string. 

    How can I do that? 

    thanks

    Just make a StringBuilder to put the results in, and use a 'lock' block around the 'Append' method so that only one thread is writing to the SB at any given time.
    • 답변으로 표시됨 smetah 2012년 4월 17일 화요일 오후 1:36
    •  
  • 2012년 4월 13일 금요일 오후 3:21
     
     

    Thanks. Will try to figure out what that translates into code...

  • 2012년 4월 16일 월요일 오전 5:30
     
     

    Hi,

    Does anybody find the solution?

  • 2012년 4월 16일 월요일 오후 1:56
     
     

    Hi,

    Does anybody find the solution?

    Yes, and links to the examples were posted here.  You can find several different examples on MSDN looking around the various properties/methods related to input/output redirection on MSDN.
  • 2012년 4월 17일 화요일 오후 1:37
     
     

    Thanks. Will try to figure out what that translates into code...

    I gave up. 
  • 2012년 4월 17일 화요일 오후 1:55
     
     

    Could you post what you had so far?

    Did you have trouble subscribing to all of the events?  

    Did you have problems with the synchronization and therefore having race conditions?

    Did you have trouble getting a StringBuilder to be shared between the two events?

    Something else?

  • 2012년 4월 17일 화요일 오후 1:59
     
     답변됨

    thanks for your inquiry. 

    I gave up going along this path because it's over and beyond my capability, and I found the following simple enough for me. 

    http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file-in-dos

    • 답변으로 표시됨 smetah 2012년 4월 17일 화요일 오후 1:59
    •