Peter
Peter

Reputation: 7804

In Visual Studio 2013 how do I dump the contents of a variable to a file while debugging?

I have a varible of type List that is being returned from a call to Entity Framework and a bunch of other processing before being dropped into a variable that is then serialised into JSON etc etc.

It would be really handy if I could grab the data out of each vairable along the way to analyse where things are going wrong (or right for that mater)

For variables with a little data the immediate window is fine but the variable i'm currently playing with has over 1000 lines of data in it that would be much easier to filter if I could get it into a spreadsheet or the like.

I'd rather not pepper my code with Console.WriteLines or other Trace if I can help it.

So is there some trick or extension or simply some code I can type into the immediate or command window do get this done?

I'm thinking that these might be the way to go but it's not quite gelling for me.

data.ForEach(Console.WriteLine); 

or

File.WriteAllLines("C:\temp", data);

Link to the Immediate Window

EDIT

Here are some examples of what is not working

    [Test]
    public void ImmediateWindowTest()
    {
        var data = new List<dynamic> { new { Z = "A", Y = 1 }, new { Z = "B", Y = 2 } };

        // System.IO.File.WriteAllText (@"c:\temp\foo.txt", data);  
        // -- The best overloaded method match for 'System.IO.File.WriteAllText(string, string)' has some invalid arguments

        // System.IO.File.WriteAllLines(@"c:\temp\foo.txt", data); 
        // -- The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])' has some invalid arguments

        // System.IO.File.WriteAllLines(@"c:\temp\foo.txt", data.Select(p=>String.Format("{0}, {1}", p.Z, p.Y)); 
        // -- Expression cannot contain lambda expressions

    }

Upvotes: 3

Views: 878

Answers (2)

Omer Raviv
Omer Raviv

Reputation: 11827

I work on a commercial Visual Studio extension that can probably help you achieve what you need to do:

If you need to Filter the list, you can use OzCode's Filter. If you want to do a text search on the values of the fields, you can use OzCode's Search. If you'd like to just export the object graph to a file, that isn't yet directly supported, but after doing a Search you will find a JSON file under %TEMP%\OzCode which will contain all the data (up to the depth in the object graph that you've searched).

Upvotes: 0

Jay Borseth
Jay Borseth

Reputation: 1993

I just tried the Immediate Window and it works providing you have write permission to the output file directory. So if your code is:

var a = "Lawrence\r\nLessig";

Then in the immediate Window use:

File.WriteAllText (@"c:\Users\MyUserLogin\documents\foo.txt", a);

Creates foo.txt containing:

Lawrence
Lessig

Upvotes: 3

Related Questions