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?
Here's an article that answers a part of my question: http://www.codeproject.com/KB/trace/DbMonNET.aspx
i.e. how to capture the output of the Debug/Console window. But, it seems that there's no way of getting the stack trace from this output. Looking from this perspective it looks like a bad approach anyway.
FURTHER RESEARCH: Looks like these exceptions are appearing because they are handled in some other dll that is not linked properly, and they are handled there instead of my try/catch blocks. This is probably the place where I should be looking my error for i.e. where there's a dll reference I should instead add a project reference.
MORE RESEARCH: Enable breaking at exceptions in Visual Studio main menu: Debug -> Exceptions -> Check the type of exceptions you want the application to break at(Common Language Runtime)...There's no better way to deal with exceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With