In the following test program, the error output is correctly written to Error.txt.
using System;
using System.IO;
public class Test{
public static void Main(string[] args){
Console.SetOut(new StreamWriter("Output.txt", true));
Console.SetError(new StreamWriter("Error.txt", true));
int[] test = new int[1];
Console.Error.WriteLine(test[0]);
}
}
However, if we change the line
Console.Error.WriteLine(test[0]);
to
Console.Error.WriteLine(test[7]);
which will cause an exception, the error message for this exception gets printed to the console instead of to the file. How can I programmatically set it up so that the error message for system-thrown exceptions is also redirected to a file?
Simple console redirection (2> or >) is not an option because of the context in which this program is run.