views:

54

answers:

2

I'm going to precompile an asp.net application in my custom c# form. How do i retrieve the process logs and check whether it is a successful process or not?

Here's my 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);

Thanks!

+2  A: 

In your source application register for the ErrorDataReceived event:

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

Any error thrown in the target application can write data into this. Something like this:

Console.Error.WriteLine( errorMessage ) ;
Bharath K
+2  A: 

Set your ProcessStartInfo.RedirectStandardOutput to true - this will redirect all output to Process.StandardOutput, which is a stream that you can read to find all output messages:

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

You can also use the OutputDataReceived event in a similar way to what @Bharath K describes in his answer.

There are similar properties/events for StandardError - you will need to set RedirectStandardError to true as well.

Oded
ProcessStartInfo class doesn't have a method called Start() and StandardOutput()?
Martin Ongtangco
@Martin Ongtangco - No, it doesn't. `Process` does. See here: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo_methods.aspx
Oded
Hi, i'm getting this error but no details to help me out..."The system cannot find the file specified"
Martin Ongtangco
im pretty sure the file exists.
Martin Ongtangco