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

发布于 2024-07-07 08:17:41 字数 38 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖不如嘢 2024-07-14 08:17:41

如果您尝试在运行时而不是在调试跟踪中获取会话的大小,您可能需要尝试如下操作:(

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

灵感来自 http://www.codeproject.com/KB/session/exploresessionandcache.aspx)

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 = 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)

千紇 2024-07-14 08:17:41

上面答案中的代码一直给我相同的数字。 这是最终对我有用的代码:

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));
}

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));
}
千紇 2024-07-14 08:17:41

我认为您可以通过将 Trace="true" 添加到 aspx 页面的页面指令来找到该信息。 然后,当页面加载时,您可以看到有关页面请求的大量详细信息,包括我认为的会话信息。

您还可以通过在 web.config 文件中添加一行来在整个应用程序中启用跟踪。 就像是:

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

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"/>
御弟哥哥 2024-07-14 08:17:41

这是我的代码,用于将所有当前会话变量及其大小(以 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

This is my code for getting all current Session variables with its size in kB into a Dictionary.

// <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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文