0

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.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • possible duplicate of [Is there a way to continuously mirror the result of Console.Write to a collection (array,list,etc)?](http://stackoverflow.com/questions/5368229/is-there-a-way-to-continuously-mirror-the-result-of-console-write-to-a-collectio) – Hans Passant Jul 17 '12 at 09:16

3 Answers3

2

I don't think the Console.SetError method actually changes the "DOS" error output of the application, just where Console.Error outputs. You should try PInvoking SetStdHandle. For example of this, see here: Redirect stdout+stderr on a C# Windows service

Community
  • 1
  • 1
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • You don't think there's a way to do it in C# without invoking native function calls? – merlin2011 Jul 17 '12 at 03:31
  • standard output/error output is usually done by the process that spawned the process whose output is being redirected. This is supported through things like ProcesStartInfo.RedirectStandardOutput. This is because that process has no control over how/what is output by the process it is spawning. The process itself has complete control over where its output goes; so it generally has no need to redirect its own output--it can just put the output where ever it wants. – Peter Ritchie Jul 17 '12 at 14:51
0

I believe, error output doesn't work the way you try using it. It is just a way to output information about some errors. Look at this http://msdn.microsoft.com/en-us/library/system.console.seterror.aspx

If you want to write something about an error, you need to do the next:

Console.Error.WriteLine("Error Log for Application {0}", appName);

so if you want to write info about exceptions to stderr, you need to catch your exceptions and direct its messages to stderr.

...
catch(Exception e) { Console.Error.WriteLine(e.Message); }
Eugene Petrov
  • 651
  • 1
  • 5
  • 14
0

Are you looking for something like this?

FileStream errorFileStream = new FileStream("Error.txt", FileMode.OpenOrCreate);
StreamWriter errorStreamWriter = new StreamWriter(errorFileStream);
Console.SetError(errorStreamWriter);

string[] test = new string[2];
test[0] = "Hello";
test[1] = "World";

try
{
    Console.WriteLine(test[2]);
}
catch (Exception ex)
{
    Console.Error.WriteLine(ex.Message);
}

Console.Error.Close();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
danish
  • 5,550
  • 2
  • 25
  • 28