none
Problem redirect debug output to a file using trace listener RRS feed

  • Question

  • 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 is 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.

    Anyone have any ideas?


    Bojan Skrchevski
    Wednesday, April 6, 2011 4:20 PM

Answers

  • Ok. This looks like the exception is in some dll and it's handled in the dll itself. See, suppose you call a function 'A' in a dll, which in turn makes lot of internal calls (A calls 'B' which calls 'C' ). In call stack you will see only the A at the top level of call stack.

    If you want to get call stack for dll as well, then instead of adding the dll reference, add the project reference.

     


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, April 7, 2011 4:28 PM

All replies

  • Hello Bojan,

    You are doing nothing wrong. Just add following two lines at the end. That's it.

    Debug.WriteLine(Environment.StackTrace);
    debugFile.Close();
    

     Hope this helps you.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, April 6, 2011 6:34 PM
  •  

    Hello Adavesh,

     

    Thanks for the answer.

    Do you mean at the end of the usage, like on application close, or on the end of the code block?


    Bojan Skrchevski
    Wednesday, April 6, 2011 6:44 PM
  • You can add Debug.WriteLine() anywhere you want to record Stack trace. But make sure you close the Stream.

    (Here you cannot close the Stream in SomeMethod() because, you won't able to write the stack trace after that because you will not have any Stream to write the data to file !!!)

    So, You do one thing. Declare Stream object at class level. Then you close it in Dispose method.

    Let me know if you have any questions.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, April 6, 2011 6:56 PM
  • Hello again,

     

    Looks like I don't understand something in the way that these listeners work, or I'm asking the question in wrong way.

    The thing is that I already have a logging system in my application, and that logs exceptions in a .log file. try...catch...and log.

    That's all fine. If there is an exception it will catch it and save it in the .log file.

     

    But, there are a bunch of exceptions that are not caught by this system of mine and they are showing up in the Debug/Console window.

    "A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll

    A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll"

    I want to analyse these exceptions and see where they're coming from.

    This is why I thought that I could just attach a text listener to the Debug listeners and it will automatically write the stuff in a file(with the stack trace information).


    Bojan Skrchevski
    Thursday, April 7, 2011 3:20 PM
  • Ok. This looks like the exception is in some dll and it's handled in the dll itself. See, suppose you call a function 'A' in a dll, which in turn makes lot of internal calls (A calls 'B' which calls 'C' ). In call stack you will see only the A at the top level of call stack.

    If you want to get call stack for dll as well, then instead of adding the dll reference, add the project reference.

     


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, April 7, 2011 4:28 PM
  • I see. So instead of looking for a solution like redirecting the output to a file with a stack trace, I should be linking the assemblies in my project correctly.

    Thank you very much for this Adavesh. You have been very helpfull.


    Bojan Skrchevski
    Thursday, April 7, 2011 4:40 PM
  • You are welcome :)


    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, April 7, 2011 5:40 PM