Kev'n Roberts

Session Dump Utility

I'm currently working on a project where we are storing quite a bit of data in Session for the user. Naturally, QA had a hard time testing whether or not something actually was stored in Session. So, we wrote a little page that showed the values of what we put in Session. The problem with this approach is that every time we added another item to Session, or removed an item, we would have to fix this page to include the new and remove the old. It got to be very tiresome.

I had just finished doing some work with reflection (my first time ever). And it occurred to me that I could use reflection to show everything that was stored in Session in such a way that it could be written once and never need to be updated.

Here's the basic rundown of what the utility does (see attached .zip file):

  1. On the page load, iterate over all items in Session
  2. Print the Key of the item
  3. If the value of the item is a native data type (int, bool, decimal -- including String and DateTime objects) print the value
  4. If the value is an object...
  5. Iterate over all the public properties of the object
  6. Print the Property Name
  7. If the value of the Property is a native data type, print it
  8. If it is an object, GOTO #5

In the end, I always get to a native data type (or String or DateTime), so we wouldn't end up in an endless loop. Here's a sample of what it displays:

 

As an added bonus, it will also display the approximate size of all the objects (in bytes/kilobytes).

This utility has been very helpful for me, to make sure I know what is in Session, without having to step through the code in debug mode and stop and interrogate the Session object to find the specific value (or values) I'm looking for. This has also been very helpful to QA so that they can see what is put into Session.
 

Attachment: SessionDump.zip
Posted: May 04 2008, 09:12 PM by kevnroberts | with 4 comment(s)
Filed under: ,

Comments

Ryan Ternier said:

THe one thing I wish you could do as a developer is look at what everyone has in their own sessions... Even if you had to do this from the ISS Server itself. It would be amazing to switch a value in another person's session rather than force an update to a DataBase only to call an event on their next postback :\

# May 5, 2008 1:45 AM

Nawaz Ali said:

Hi,

Its a nice little piece of code, I was wondering if you could use the APPLICATION-LEVEL TRACE LOGGING?

Isnt it what could also be used for this purpose with <trace enabled="true" /> tag?

Nawaz

# May 5, 2008 3:06 AM

kevnroberts said:

Hi Nawaz,

You can use the <trace enabled="true" /> which will show you a lot of information. It shows you what is in Session, but only at the top level. In other words, if I put an object in Session, it will simply call the "ToString()" method of the object, which will just give me the type of object it is. It does not read all the properties of the object and display them as well.

# May 5, 2008 8:24 AM

Nis L. Simonsen said:

Quick thought: Given that the stuff you put in session is implicitly serializable, why not just let an XmlSerializer do the grunt work?

I use the code below for dumping an object into a text file (this is just some stub code, so performance is not an issue); you could modify it to dump to the output stream instead:

public static void SerializeToFile(string filepath, object instance)

{

XmlSerializer serializer = new XmlSerializer(instance.GetType());

Stream writer = new FileStream(filepath, FileMode.Create);

serializer.Serialize(writer, instance);

writer.Close();

}

(Again, just a quick thought, I might have missed a point or two :) )

# May 5, 2008 9:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)