Process.Start에서 로그를 얻는 방법
-
18-09-2020 - |
문제
내 사용자 지정 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.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
또한.
다른 팁
원본 응용 프로그램에서 errorlyAteRecived 이벤트에 대한 등록 :
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( );
.
대상 응용 프로그램에서 Throw되는 오류는 데이터를 다음에 쓸 수 있습니다.이와 같은 것 :
Console.Error.WriteLine( errorMessage ) ;
. 제휴하지 않습니다 StackOverflow