johnvisiomvp

Life with Visio and other Microsoft Toys!

Formatting Debug Writeline

with one comment

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

Advertisements

Written by johnvisiomvp

October 12, 2013 at 10:31 am

One Response

Subscribe to comments with RSS.

  1. That’s an AWESOME debugging tip to format the output in columns to quickly cherry pick off data points! Nice! Thanks for the tip!

    phenry9999

    October 16, 2013 at 3:07 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: