如何捕获wget进程的错误? [英] How to trap error of wget process?

查看:45
本文介绍了如何捕获wget进程的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

am doing application of downloading images using wget process in c#. But while downloading images I am providing type of image i.e. .jpeg or .png. Through wget command. If while downloading I am passing .jpeg and if .jpeg is not present, Then I want to trap error "file not found" through process class. But it is not happening.

我的代码如下:

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 不存在,则在 erroe.log 中出现找不到文件".但是通过进程类没有捕获错误,即在 ProcessErrorData e.Data 总是为空.那么我如何捕获错误还有其他方法吗?

In above code if jpeg is not present then in erroe.log coming "file not found". But through process class not trapping error i.e. in ProcessErrorData e.Data is always coming null. So how can I trap error is there any other way?

感谢任何帮助.

推荐答案

wget(在 1.12 以上的版本中)确实返回了可靠的 退出代码.

wget (in versions above 1.12) does return a reliable exitcode.

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

0 No problems occurred.
1 Generic error code.
2 Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’...
3 File I/O error.
4 Network failure.
5 SSL verification failure.
6 Username/password authentication failure.
7 Protocol errors.
8 Server issued an error response.

在 1.12 之前,您遇到了麻烦:

Prior to 1.12 you're in trouble:

在 1.12 之前的 Wget 版本中,Wget 的退出状态往往无用且不一致.无论遇到什么问题,递归下载实际上总是返回 0(成功),并且非递归获取只返回与最近尝试下载相对应的状态.

In versions of Wget prior to 1.12, Wget’s exit status tended to be unhelpful and inconsistent. Recursive downloads would virtually always return 0 (success), regardless of any issues encountered, and non-recursive fetches only returned the status corresponding to the most recently-attempted download.

进程的退出代码被传递回 ExitCode 属性.为了方便起见,您应该利用它并保留(错误)日志记录.

The exitcode of the process is handed back to the Process instance in the ExitCode property. You should leverage that and keep the (error) logging for convenience.

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

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

If you absolutely want to respond to the error messages that are logged (either to standardoutout or erroroutput), make sure you check the correct strings: If the log shows ERROR 404: Not Found then this message.Contains("file not found") will never be true. This is a reason I always tend to stay away from logfile parsing.

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

Do notice that processes are not required to write errormessage to the standard error stream. The errormessages can very well come in the standard output stream as well...

这篇关于如何捕获wget进程的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆