Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 save array/collection data to a file while debugging

Is there some way to save array/list/collection data to a file while debugging in VS2010?

For example, in this code:

var addressGraphs = from a in context.Addresses
                    where a.CountryRegion == "Canada"
                    select new { a, a.Contact };

foreach(var ag in addressGraphs) {
   Console.WriteLine("LastName: {0}, Addresses: {1}", ag.Contact.LastName.Trim(),
                     ag.Contact.Addresses.Count());



   foreach(var Address in ag.Contact.Addresses) {
      Console.WriteLine("...{0} {1}", Address.Street1, Address.City);
   }
}

I'd like to set a breakpoint on the first 'foreach' line and then save the data in 'addressGraph' to a file.

where 'a' contains fields such as:

   int addressID
   string Street1
   string City
   <Ect.>

and 'Contact' contains fields such as:

   string FirstName
   string LastName
   int contactID
   <Ect.>

I'd like the file to contain the values of each of the fields for each item in the collection.

I don't see an obvious way to do this. Is it possible?

like image 781
casterle Avatar asked Aug 25 '11 17:08

casterle


4 Answers

When your breakpoint is hit, open up the Immediate window and use Tools.LogCommandWindowOutput to dump the output to a file:

>Tools.LogCommandWindowOutput c:\temp\temp.log
?addressGraphs
>Tools.LogCommandWindowOutput /off

Note: You can use Log which is an alias for Tools.LogCommandWindowOutput


Update: The > character is important. Also, the log alias is case sensitive. See screenshot:

enter image description here

like image 102
Mrchief Avatar answered Nov 11 '22 04:11

Mrchief


I also encoutered such a question, but in VS2013. I have to save a content of array while debugging.

For example, I need to save a content of double array named "trimmedInput". I do so:

  1. Open QuickWatch Window from Debug menu (Ctrl+D, Q). enter image description here

  2. Put your variable in Expression and push Recalculate Button enter image description here

  3. You'll see all the values. Now you could select them all (Ctrl+A) and copy (Ctrl+C).

  4. Paste (Ctrl+V) them in your favorite editor. Notepad, for example. And use them. enter image description here

That's the simples way that I know. Without additional efforts. Hope that my description helps you!

P.S. Sorry for non English interface on screenshots. All necessary information are written in the text.

like image 34
Anton Bakulev Avatar answered Nov 11 '22 06:11

Anton Bakulev


Something similar is possible with this method:

I built an extension method that I use in all of my projects that is a general and more powerful ToString() method that shows the content of any object. I included the source code in this link: https://rapidshare.com/files/1791655092/FormatExtensions.cs

UPDATE: You just have to put FormatExtensions.cs in your project and change the Namespace of FormatExtensions to coincide to the base Namespace of your project. So when you are in your breakpoint you can type in your watch window: myCustomCollection.ToStringExtended()

And copy the output wherever you want

like image 2
Durden81 Avatar answered Nov 11 '22 04:11

Durden81


On Visual studio Gallery search for: Object Exporter Extension.
be aware: as far as I worked with, it has a bug that block you from exporting object once in a while.

like image 1
Ehsan Abidi Avatar answered Nov 11 '22 04:11

Ehsan Abidi