Problem redirecting debug output to a file using trace listener
Asked Answered
P

1

5

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?

Placard answered 6/4, 2011 at 15:7 Comment(5)
Are you sure you should be adding to Debug.Listeners? Calling Console.WriteLine and Debug.WriteLine produce different behaviors. If you are adding a Listener to Debug, but calling Console, it might cause the issue you're reporting.Waxen
@Waxen - I'm not sure what exactly do you mean. What I'm trying to do here is to have the same output that I have in the Debug console window in Visual Studio to a file, but with a call stack for each exception that happens there.Placard
You should try manually writing to your Debug listener by calling your function to set it up, then calling Debug.WriteLine("Debugger Initialized"); That should verify that everything is working. If you're just relying on the framework, you can't be sure which stream the output window is actually listening to.Waxen
As a note, I implemented your method, then called Debug.WriteLine, and my message showed up in the text file. I suspect you need to override Console output or something else to capture what VS is capturing... EDIT: Actually, you should just have a top-level error handler that fails the application gracefully, writing the call stack to a log and displaying a friendly message to the user.Waxen
@Waxen - I do have a global error handler that is not catching some of the errors that show up in the Debug/Console window, I'm trying to find out what they are and where to find them, this is the whole purpose I'm doing this.Placard
P
2

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.

Placard answered 7/4, 2011 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.