2010-07-21 107 views
3

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

这里是我的代码

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

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

您还可以使用类似于@Bharath K在他的回答中所描述的那样的OutputDataReceived事件。

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

+0

ProcessStartInfo类没有称为Start()和StandardOutput()的方法? – 2010-07-21 05:59:14

+0

@Martin Ongtangco - 不,它不。 '进程'。看到这里:http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo_methods.aspx – Oded 2010-07-21 06:15:01

+0

嗨,我得到这个错误,但没有细节,以帮助我... “该系统找不到指定的文件“ – 2010-07-21 06:26:57

2

在为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) ;