Formatting Debug Writeline

For the past few months, I have been working on a Windows Phone app that uses several tables. Most of the time when I debug, I rely heavily on Debug.Writeline to help me keep track of what is going on as I test an app.  If the app crashes, I can look at Output and get a better idea where the fail occurred than trying to wander back through the Visual Studio trace. In addition to the routine names I place in the Writeline statements, I include key values, so that I can read the history of the app before the crash. The only problem is that though I use Debug.Writeline(“[Teststring]Code={0} Name={1}”,LangCode, LangName); the code is free format and it gets hard to read a table when you encase a line like that in a foreach if the length of an element like LangCode can vary between two to five characters.

Luckily there is an easy way to format items within Debug.Writeline without using Format. I can replace {0} in the Debug.Writeline with {0,-5}. The “5” sets the length of the output as five characters for the first item and the second items all line up. The minus sign indicates that the string is left justified. There are other formatting codes for numbers and dates. You can find more information on MSDN about Composite Formatting at http://msdn.microsoft.com/en-us/library/txafckwd.aspx It does not explicitly mention Debug.Writeline, but the information does apply. So if you need to print tables using Debug.Writeline to help with your debugging, try it out and check out what can be done with numerics and dates.

Though this information is out there, it is not easy to find. Luckily when I found this technique several years ago, I made a reference in OneNote. OneNote is an excellent tool for organizing trivia and I was able to quickly refind the link.

John Marshall… Visio MVP       Visio.MVPs.org

Published by johnvisiomvp

The original Visio MVP. I have worked with the Visio team since 1993

One thought on “Formatting Debug Writeline

Comments are closed.