views:

3094

answers:

4

Hello!

How to find out size of session in ASP.NET from web application?

Thanks

A: 

Hello, if you mean Session Timeout, the default timeout is 20 minutes. Here it is a document that can help you to dectect it http://aspalliance.com/520.

If you want just make it bigger or check your server default, you can check the file web.config.

Victor

VP
A: 

Nope. Not session timeout but session size (in kB for example).

GrZeCh
+1  A: 

I think you can find that information by adding Trace="true" to the page directive of a aspx page. Then when the page loads you can see a large number of details regarding the page request, including session information i think.

You can also enable tracing in your entire application by adding a line to your web.config file. Something like:

<trace enabled="true" requestLimit="10" pageOutput="true" traceMode="SortByTime" 
 localOnly="true"/>
Rafe
+10  A: 

If you're trying to get the size of Session during runtime rather than in debug tracing, you might want to try something like this:

long totalSessionBytes;
BinaryFormatter b = new BinaryFormatter();
MemoryStream m;
foreach(var obj in Session) 
{
  m = new MemoryStream();
  b.Serialize(m, obj);
  totalSessionBytes += m.Length;
}

(Inspired by http://www.codeproject.com/KB/session/exploresessionandcache.aspx)

ddc0660
Thanks. That was what I needed.
GrZeCh
I needed to make the following changes:long totalSessionBytes = 0;since m.Length returns a long. But aside from that it's a nice concise piece of code! The loop can be foreach, as well. ;-)
Oliver
@Oliver Thanks for the feedback. I made the adjustments you suggested. Looks a little nicer now.
ddc0660