Whenever you have a problem in your code, it is always a good idea to know what is going on inside. The class System.Diagnostics.Debug in .Net Framework will help you a lot in this task.
The first advantage of the Debug class is that it produces code only if you build your application in Debug mode. When you build your application in Release mode, no code will be generated from the Debug calls.
Debug in a text file
At the beginning of your application, your must add a TextWriterTraceListener to the Listeners list of the Debug class.
All the Debug code produced will be outputed in the Visual Studio console AND in the text file you chose.
If the file is always the same:
The output will be appended to the file every time AND a new file starting with a GUID then your filename will be generated.
Debug in the console
Indenting your debug output
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0