Comment obtenir le journal de Process.Start
-
18-09-2020 - |
Question
Je vais précompiler une application asp.net dans mon formulaire C# personnalisé.Comment puis-je récupérer les journaux de processus et vérifier s'il s'agit d'un processus réussi ou non ?
Voici mon 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);
Merci!
La solution
Définissez votre ProcessStartInfo.RedirectStandardOutput
à true
- cela redirigera toutes les sorties vers Process.StandardOutput
, qui est un flux que vous pouvez lire pour trouver tous les messages de sortie :
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();
Vous pouvez également utiliser le OutputDataReceived
événement d'une manière similaire à ce que @Bharath K décrit dans sa réponse.
Il existe des propriétés/événements similaires pour StandardError
- vous devrez définir RedirectStandardError
à true
aussi.
Autres conseils
Dans votre demande source Inscrivez-vous pour l'événement errordronnaAreceved:
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( );
Toute erreur lancée dans l'application cible peut écrire des données dans cette option.Quelque chose comme ça:
Console.Error.WriteLine( errorMessage ) ;