如何捕获wget过程中的错误



am在c#中使用wget进程下载图像的应用程序。但在下载图像时,我通过wget命令提供图像类型,即.jpeg或.png。如果在下载时我正在传递.jpeg,如果.jpeg不存在,那么我想通过进程类捕获错误"找不到文件"。但这并没有发生。

我的代码如下:

class TrapProcessError
{
    private Process process = new Process();
    public TrapProcessError()
    {
    }
    public void Start()
    {
        string args= "-r -c -np --retry-connrefused --tries=0 "url containing image folder" -P C:\Images --cut-dirs=2 -nH -A jpeg -o C:\Images\error.log";
        string filename= "path of wget\wget.exe";
        process.StartInfo.FileName = filename;
        process.StartInfo.Arguments = args;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.ErrorDataReceived += this.ProcessErrorData;
        process.OutputDataReceived += this.ProcessOutputData;
        process.Start();      
        process.BeginErrorReadLine();  
        process.BeginOutputReadLine();
        process.WaitForExit();
    }
    private void ProcessErrorData(object sender, DataReceivedEventArgs e)
    {
       string message = e.Data;                    
        if (message.Contains("file not found"))
        {
           Console.WriteLine("Error :" +message);
           process.Close();              
        }     
    }
    private void ProcessOutputData(object sender, DataReceivedEventArgs e)
    {
        string message = e.Data;
       Console.WriteLine("Output :" +message);  
     }
    public static void Main(string[] args)
    {
        TrapProcessError trapProcessError= new TrapProcessError();
        trapProcessError.Start();
    }
}

在上面的代码中,如果jpeg不存在,那么在错误日志中会出现"找不到文件"。但是通过进程类没有捕获错误,即ProcessErrorData e中的错误。数据总是为空。那么,我如何捕捉错误呢?还有其他方法吗?

感谢您的帮助。

wget(在1.12以上的版本中)确实返回了可靠的exitcode。

0没有出现任何问题
1一般错误代码
2分析错误——例如,在分析命令行选项时,".wgetrc"或".netrc"…
3文件I/O错误
4网络故障
5 SSL验证失败
6用户名/密码身份验证失败
7协议错误
8服务器发出错误响应。

1.12之前你有麻烦了:

在1.12之前的Wget版本中,Wget的退出状态往往是无益和不一致的。无论遇到什么问题,递归下载几乎总是返回0(成功),而非递归获取只返回与最近尝试的下载相对应的状态。

进程的exitcode会在exitcode属性中返回给进程实例。为了方便起见,您应该利用它并保留(错误)日志记录。

public void Start()
{
    string args= "-r -c -np --retry-connrefused --tries=0 "url containing image folder" -P C:\Images --cut-dirs=2 -nH -A jpeg -o C:\Images\error.log";
    string filename= "path of wget\wget.exe";
    process.StartInfo.FileName = filename;
    process.StartInfo.Arguments = args;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.ErrorDataReceived += this.ProcessErrorData;
    process.OutputDataReceived += this.ProcessOutputData;
    process.Start();      
    process.BeginErrorReadLine();  
    process.BeginOutputReadLine();
    process.WaitForExit();
    if (process.ExitCode > 0) 
    {
         // do what you need to do in case of an Error
         Console.WriteLine("Error occured:{0}", process.ExitCode);
    }
}

如果您绝对希望响应记录的错误消息(到standardoutout或erroroutput),请确保检查正确的字符串:如果日志显示ERROR 404: Not Found,则此message.Contains("file not found")永远不会为真。这就是我总是倾向于远离日志文件解析的原因。

请注意,进程不需要将errormessage写入标准错误流。错误消息也可以出现在标准输出流中。。。

相关内容

最新更新