How to trap error of wget process?

2019-08-08 13:51发布

问题:

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.

My code is below:

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

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?

Any Help is appreciated.

回答1:

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

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.

Prior to 1.12 you're in trouble:

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.

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

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...



标签: c# wget