プロセスからログを取得する方法。スタート
-
18-09-2020 - |
質問
私はプリコンパイルするつもりですasp.net 私のカスタムc#フォームでのアプリケーション。プロセスログを取得し、それが成功したプロセスであるかどうかを確認するにはどうすればよいですか?
ここに私のコードがあります
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.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();
また、以下を使用することもできます OutputDataReceived
@Bharath Kが彼の答えで説明しているものと同様の方法でイベント。
のための同様のプロパティ/イベントがあります StandardError
-設定する必要があります RedirectStandardError
に true
同様に。
他のヒント
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 ) ;
. 所属していません StackOverflow