Search code examples
visual-studio-2013visual-studio-debugging

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

    }

Solution

  • 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