I have created a debug listener to redirect the output from the Debug/Console window to a file(with a call stack), using the following code:
void SomeMethod()
{
// Create a file for output .txt.
Stream debugFile = File.Create(fileName);
// create TextWriterTraceListener named "file"
TextWriterTraceListener debugWriter = new TextWriterTraceListener(debugFile, "file");
// add to debug listeners
Debug.Listeners.Add(debugWriter);
// set callstack to be shown
Debug.Listeners["file"].TraceOutputOptions |= TraceOptions.Callstack;
// set auto-flush
Debug.AutoFlush = true;
}
but the output won't redirect to the file I specified, it's always empty.
I am calling this from the constructor in my main form. Is the place where I'm calling it from a problem?
What I am trying to achieve here is to have the exceptions from the Debug output window placed in a file with a call stack, so that I can find them and correct them.
UPDATE: After some research I came to a conclusion that adding a new TraceListener
to the Debug Listeners
collection does not redirect the output from the Debug/Console. It is actually just responding to Write
, WriteLine
etc. methods as does the default listener.
The problem still remains: How to capture the output of the Debug/Console window and how to get the stack trace of the exceptions that appear there?
Anyone have any ideas?