views:

127

answers:

2

Is there a way in Visual Studio (2008 if it matters) that I can, in debug/break mode, write the contents of a variable to a text/XML file?

The scenario is that I have a long process running in debug and I have realised too late that I haven't logged enough detail about the events that the process has been monitoring, but fortunately a history is still available within a variable in the code.

I could trawl through the tens of thousands of items in this list, but it's not going to persist once I hit stop on the app ... there is no obvious context option for this, but is there any way, a better way than manual? Or is there no hope and I just need to hit stop, re-tool the logging function and run the thing again?

Aside from trying to hit a break point, modify the code and re-write to make a better logger, is there a way of not losing that in-memory data?

+4  A: 

Hi,

Ha ha, I sympathsize with your problem! Next time add some logging first :)

One way to do it would be to use the immediate window. (Menu: Debug-Windows-Immediate). In the window that appears you can use the "?" to query the value of a variable.

Assuming your history variable is a string you view it's contents by typing the following in the immediate window:

?history

You could copy and paste the output from there into a text file or alternatively ask visual studio to log all command window output. To do this type:

>log "c:\test.log"
?history
>log off

Log is an alias for Tools.LogCommandWindowOutput and accepts the following parameters:

Tools.LogCommandWindowOutput [filename] [/on|/off] [/overwrite]

Check out this MSDN link for more information.

Hope that helps

JamesPickrell
A: 

I think that my answer is pretty much the same as JamesPickrell's, but from the Immediate Window you could also do something like this:

My.Computer.FileSystem.WriteAllText("c:\temp.txt",history,True)

This would output the content of the "history" variable to a file called c:\temp.txt.

Richard