2010-07-21 103 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) ;