[英]How to get log from Process.Start
I'm going to precompile an asp.net application in my custom c# form. 我将在我的自定义c#表单中预编译一个asp.net应用程序。 How do i retrieve the process logs and check whether it is a successful process or not?
如何检索进程日志并检查它是否是成功的进程?
Here's 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);
Thanks! 谢谢!
Set your ProcessStartInfo.RedirectStandardOutput
to true
- this will redirect all output to Process.StandardOutput
, which is a stream that you can read to find all output messages: 将
ProcessStartInfo.RedirectStandardOutput
设置为true
- 这会将所有输出重定向到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();
You can also use the OutputDataReceived
event in a similar way to what @Bharath K describes in his answer. 您也可以使用与@Bharath K在其答案中描述的方式类似的方式使用
OutputDataReceived
事件。
There are similar properties/events for StandardError
- you will need to set RedirectStandardError
to true
as well. StandardError
有类似的属性/事件 - 您还需要将RedirectStandardError
设置为true
。
In your source application register for the ErrorDataReceived event: 在您的源应用程序寄存器中,为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( );
Any error thrown in the target application can write data into this. 目标应用程序中抛出的任何错误都可以将数据写入此中。 Something like this:
像这样的东西:
Console.Error.WriteLine( errorMessage ) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.