Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output a watched Visual Studio variable to a file

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 application ... 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 breakpoint, modify the code and re-write to make a better logger, is there a way of not losing that in-memory data?

like image 371
Unsliced Avatar asked Aug 26 '09 08:08

Unsliced


People also ask

How do I export output from Visual Studio?

Others can import it to the same source and debug it further. To export the Data Tips, go to the Visual Studio menu, Debug | Export DataTips..., which will open the Export DataTips dialog. Select the folder where you want to save the XML file, give the file a name, and click Save.

How do I get the value of a variable in Visual Studio?

The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.

How do I view output in Visual Studio?

The Output window displays status messages for various features in the integrated development environment (IDE). To open the Output window, on the menu bar, choose View > Output, or press Ctrl+Alt+O.


2 Answers

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 its 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 the MSDN article Log Command Window Output Command for more information.  

like image 103
JamPickle Avatar answered Sep 22 '22 03:09

JamPickle


Thanks to Richard's answer, this is working for me.

System.IO.File.WriteAllBytes(@"c:\Temp\temp.txt", myVar);

Make sure that C:\Temp exists.

The reason for writing to a folder and not to the root C:\ is to avoid UnauthorizedAccessException when not running Visual Studio as administrator.

like image 28
angularsen Avatar answered Sep 21 '22 03:09

angularsen