20

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?

1
  • To clarify, I need to be able to do this on the fly, on an arbitrary collection. Having to add code to the app does not meet my needs. Commented Aug 25, 2011 at 18:07

6 Answers 6

30
+25

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

Sign up to request clarification or add additional context in comments.

11 Comments

I couldn't get 'Log' to work in the immediate window, but it does work in the command window.
@casterle: Did you type > character? That's important. That character tells the Immediate window that you're entering a command.
I couldn't get 'Log' to work in the immediate window, but it does work in the command window. In either window the ? command displays: code {System.Data.Objects.ObjectQuery<Chapter_2_Console_App.Contact>} base {System.Data.Objects.ObjectQuery}: {System.Data.Objects.ObjectQuery<Chapter_2_Console_App.Contact>} _name: "it" Name: "it" code but does not display the contents of the collection. What am I missings?
I did not - I thought the '>' was a command prompt. As you suggested, typing the '>' fixed the Log issue. Is there more I need to do to make the '?' command work?
The ? does not do deep serialization. The easiest way is to say ?code.[Collection] to display the contents of collection. (Note I used fake names but you get idea)
|
8

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.

Comments

1

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.

Comments

1

You can also call methods in the Immediate Window, and so I think your best bet would be to use an ObjectDumper object, like the one in the LINQ samples or this one, and then write something like this in the Immediate Window:

File.WriteAllText("myFileName.txt", ObjectDumper.Dump(addressGraph));

Depending on which ObjectDumper you decide to use, you may be able to customize it to suit your needs, and to be able to tell it how many levels deep you want it to dig into your object when it's dumping it.

5 Comments

Both of these solutions require that I change my code before I can dump the collection.
well, not neccesarily, you could load it manually by calling Assembly.Load("ObjectDumper.dll") from the Immediate Window. BTW, why do you want to save it to file?
I'm interested in viewing the content of a collection at a point in time during debug. There are a couple of reasons I'd like to be able to dump to a file (especially for large amounts of data): 1) So I can peruse and search within the data, and 2) So I can diff the data between runs or at different points in the code.
Re ObjectDumper: it doesn't do what I'm looking for, dumping thousands of lines of arcane information but, as far as I can see, none of the collection field values I'm looking for.
Re ObjectDumper, that's strange, perhaps it's because addressGraphs is a collection of an anonymous type, and so the "arcane strings" you're seeing are the compiler generated names for the anonymous type and auto properties? In any case, I updated my answer with another option.
0

Here's a solution that takes care of collections. It's a VS visualizer that will display the collection values in a grid while debugging as well as save to the clipboard and csv, xml and text files. I'm using it in VS2010 Ultimate. While I haven't tested it extensively, I have tried it on List and Dictionary.

http://tinyurl.com/87sf6l7

It handles the following collections:

•System.Collections classes  
   ◦System.Collections.ArrayList  
   ◦System.Collections.BitArray  
   ◦System.Collections.HashTable  
   ◦System.Collections.Queue  
   ◦System.Collections.SortedList  
   ◦System.Collections.Stack  
   ◦All classes derived from System.Collections.CollectionBase  

•System.Collections.Specialized classes  
   ◦System.Collections.Specialized.HybridDictionary  
   ◦System.Collections.Specialized.ListDictionary  
   ◦System.Collections.Specialized.NameValueCollection  
   ◦System.Collections.Specialized.OrderedDictionary  
   ◦System.Collections.Specialized.StringCollection  
   ◦System.Collections.Specialized.StringDictionary  
   ◦All classes derived from System.Collections.Specialized.NameObjectCollectionBase  

•System.Collections.Generic classes  
   ◦System.Collections.Generic.Dictionary
   ◦System.Collections.Generic.List  
   ◦System.Collections.Generic.LinkedList  
   ◦System.Collections.Generic.Queue  
   ◦System.Collections.Generic.SortedDictionary  
   ◦System.Collections.Generic.SortedList  
   ◦System.Collections.Generic.Stack  

•IIS classes, as used by  
   ◦System.Web.HttpRequest.Cookies  
   ◦System.Web.HttpRequest.Files  
   ◦System.Web.HttpRequest.Form  
   ◦System.Web.HttpRequest.Headers  
   ◦System.Web.HttpRequest.Params  
   ◦System.Web.HttpRequest.QueryString  
   ◦System.Web.HttpRequest.ServerVariables  
   ◦System.Web.HttpResponse.Cookies  

As well as a couple of VB6-compatible collections

Comments

0

In "Immediate Window" print following to get the binary dump:

byte[] myArray = { 02,01,81,00,05,F6,05,02,01,01,00,BA };

myArray
  .Select(b => string.Format("{0:X2}", b))
  .Aggregate((s1, s2) => s1 + s2)

This will print something like:

0201810005F60502010100BA

Change the '.Aggregate(...)' call to add blanks between bytes, or what ever you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.