如何从Process.Start获取日志

Mar*_*gco 4 c# asp.net precompiler

我将在我的自定义c#表单中预编译一个asp.net应用程序.如何检索进程日志并检查它是否是成功的进程?

这是我的代码

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);
Run Code Online (Sandbox Code Playgroud)

谢谢!

Bha*_*h K 5

在源应用程序中注册 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( );
Run Code Online (Sandbox Code Playgroud)

目标应用程序中抛出的任何错误都可以将数据写入其中。像这样的东西:

Console.Error.WriteLine( errorMessage ) ;
Run Code Online (Sandbox Code Playgroud)


Ode*_*ded 5

设置ProcessStartInfo.RedirectStandardOutputtrue- 这将重定向所有输出Process.StandardOutput,这是一个流,您可以读取以查找所有输出消息:

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();
Run Code Online (Sandbox Code Playgroud)

您也可以使用与OutputDataReceived@Bharath K在其答案中描述的方式类似的方式使用该事件.

有类似属性/事件StandardError-你需要设置RedirectStandardErrortrue为好.