【问题标题】:How to get log from Process.Start如何从 Process.Start 获取日志
【发布时间】:2010-07-21 05:48:33
【问题描述】:

我将在我的自定义 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);

谢谢!

【问题讨论】:

    标签: c# asp.net precompiler


    【解决方案1】:

    将您的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();
    

    您还可以按照@Bharath K 在他的回答中描述的类似方式使用OutputDataReceived 事件。

    StandardError 有类似的属性/事件 - 您还需要将 RedirectStandardError 设置为 true

    【讨论】:

    • ProcessStartInfo 类没有名为 Start() 和 StandardOutput() 的方法?
    • @Martin Ongtangco - 不,它没有。 Process 确实如此。见这里:msdn.microsoft.com/en-us/library/…
    • 您好,我收到此错误,但没有详细信息可以帮助我...“系统找不到指定的文件”
    • 我很确定文件存在。
    【解决方案2】:

    在您的源应用程序中注册 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 ) ;
    

    【讨论】:

      猜你喜欢
      • 2014-06-19
      • 1970-01-01
      • 2020-06-16
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多