Pergunta

Vou pré-compilar um aplicativo asp.net em meu formulário c# personalizado.Como recupero os logs do processo e verifico se o processo foi bem-sucedido ou não?

Aqui está meu código

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

Obrigado!

Foi útil?

Solução

Defina seu ProcessStartInfo.RedirectStandardOutput para true - isso redirecionará toda a saída para Process.StandardOutput, que é um stream que você pode ler para encontrar todas as mensagens de saída:

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

Você também pode usar o OutputDataReceived evento de maneira semelhante ao que @Bharath K descreve em sua resposta.

Existem propriedades/eventos semelhantes para StandardError - você precisará definir RedirectStandardError para true também.

Outras dicas

No registro do seu aplicativo de origem para o evento 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( );

Qualquer erro gerado no aplicativo de destino pode gravar dados nele.Algo assim:

Console.Error.WriteLine( errorMessage ) ;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top