简体   繁体   English

如何从 Web 应用程序中找出 ASP.NET 中的会话大小?

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

如何从 Web 应用程序中找出 ASP.NET 中的会话大小?

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:如果您想在运行时而不是在调试跟踪中获取 Session 的大小,您可能想尝试这样的事情:

long totalSessionBytes = 0;
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 ) (灵感来自http://www.codeproject.com/KB/session/exploresessionandcache.aspx

The code in the answer above kept giving me the same number.上面答案中的代码一直给我相同的数字。 Here is the code that finally worked for me:这是最终对我有用的代码:

private void ShowSessionSize()
{
    Page.Trace.Write("Session Trace Info");

    long totalSessionBytes = 0;
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = 
        new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    System.IO.MemoryStream m;
    foreach (string key in Session)
    {
        var obj = Session[key];
        m = new System.IO.MemoryStream();
        b.Serialize(m, obj);
        totalSessionBytes += m.Length;

        Page.Trace.Write(String.Format("{0}: {1:n} kb", key, m.Length / 1024));
    }

    Page.Trace.Write(String.Format("Total Size of Session Data: {0:n} kb", 
       totalSessionBytes / 1024));
}

I think you can find that information by adding Trace="true" to the page directive of a aspx page.我认为您可以通过将Trace="true"添加到 aspx 页面的页面指令来找到该信息。 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.您还可以通过在 web.config 文件中添加一行来在整个应用程序中启用跟踪。 Something like:就像是:

<trace enabled="true" requestLimit="10" pageOutput="true" traceMode="SortByTime" 
 localOnly="true"/>

This is my code for getting all current Session variables with its size in kB into a Dictionary.这是我的代码,用于将所有当前会话变量及其大小(以 kB 为单位)放入字典中。

// <KEY, SIZE(kB)>
var dict = new Dictionary<string, decimal>();

BinaryFormatter b = new BinaryFormatter();
MemoryStream m;
foreach(string key in Session.Keys) 
{
    var obj = Session[key];
    if (obj == null)
    {
        dict.Add(key, -1);
    }
    else
    {
        m = new MemoryStream();
        b.Serialize(m, obj);
        
        //save the key and size in kB (rounded to two decimals)
        dict.Add(key, Math.Round(Convert.ToDecimal(m.Length) / 1024, 2)); 
    }
}

//return dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定当前应用程序是否为 ASP.NET web 应用程序 - How to find out if the current application is an ASP.NET web app 会话在ASP.NET Web应用程序中没有超时 - Session is not timing out in ASP.NET web application 当其中存在不可序列化的对象时,如何找出ASP.NET会话的大小? - How to find out size of ASP.NET session, when there are non-serializable objects in it? 从另一个asp.net Web应用程序终止会话 - Killing a session from another asp.net web application 如何确定 ASP.Net web 应用程序是否可以处理基于 cookie 的亲和力? - How to find out if ASP.Net web application can handle cookie-based affinity? 在asp.net Web应用程序中查找有多少用户在线 - Find out how many users are online in asp.net web application 如何找出重新启动ASP.NET Web应用程序的原因 - How to find out why an ASP.NET web application is being restarted 如何在ASP.NET Web窗体应用程序和ASP.NET核心应用程序之间共享会话和Cookie? - How to share session and cookie between asp.net web form application and asp.net core application? 会话在ASP.NET Web应用程序中结束 - Session ends in ASP.NET Web application 在ASP.NET Web应用程序中,会话为null - Session is null in asp.net Web Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM