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

谢谢!

有帮助吗?

解决方案

设置你的 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 以及。

其他提示

在您的源代码应用程序注册中为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 ) ;
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top