How to get a log from Process.Start

I am going to precompile an asp.net application in my custom C # form. How to get process logs and check if this is a successful process or not?

Here is my code

string msPath = "c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\";
string msCompiler = "aspnet_compiler.exe";
string fullCompilerPath = Path.Combine(msPath, msCompiler);
msPath.ThrowIfDirectoryMissing();
fullCompilerPath.ThrowIfFileIsMissing();

ProcessStartInfo process = new ProcessStartInfo 
{ 
    CreateNoWindow = false,
    UseShellExecute = false,
    WorkingDirectory = msPath,
    FileName = msCompiler,
    Arguments = "-p {0} -v / {1}"
        .StrFormat(
            CurrentSetting.CodeSource,
            CurrentSetting.CompileTarget)
};

Process.Start(process);

Thank!

+3
source share
2 answers

Set ProcessStartInfo.RedirectStandardOutputto true- this will redirect all output to Process.StandardOutput, which is a stream you can read to find all output messages:

ProcessStartInfo process = new ProcessStartInfo 
{ 
   CreateNoWindow = false,
   UseShellExecute = false,
   WorkingDirectory = msPath,
   RedirectStandardOutput = true,
   FileName = msCompiler,
   Arguments = "-p {0} -v / {1}"
            .StrFormat(
              CurrentSetting.CodeSource, 
              CurrentSetting.CompileTarget)
};

Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();

You can also use the event OutputDataReceivedsimilarly to what @Bharath K describes in your answer.

Similar properties / events for StandardError- you need to set RedirectStandardErrorto true.

+4
source

ErrorDataReceived:

StringBuilder errorBuilder = new StringBuilder( );
reportProcess.ErrorDataReceived += delegate( object sender, DataReceivedEventArgs e )
{
    errorBuilder.Append( e.Data );
};
//call this before process start
reportProcess.StartInfo.RedirectStandardError = true;
//call this after process start
reportProcess.BeginErrorReadLine( );

, , . - :

Console.Error.WriteLine( errorMessage ) ;
+2

Source: https://habr.com/ru/post/1755662/


All Articles