6

我将在我的自定义 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);

谢谢!

4

2 回答 2

7

设置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();

您还可以OutputDataReceived以与@Bharath K 在他的回答中描述的类似方式使用该事件。

有类似的属性/事件StandardError- 您也需要设置RedirectStandardErrortrue

于 2010-07-21T05:56:25.933 回答
3

在您的源应用程序中注册 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 ) ;
于 2010-07-21T05:53:06.470 回答