Can I display contents of Application or Cache obj

2019-08-13 18:59发布

问题:

The ASP.NET WebForms trace output has a section for Application State. Is it possible to see the same using Glimpse?

In my home controller's Index() method, I tried adding some test values, but I don't see the output in any of the Glimpse tabs.

ControllerContext.HttpContext.Application.Add("TEST1", "VALUE1");
ControllerContext.HttpContext.Cache.Insert("TEST2", "VALUE2");

I didn't see anything in the documentation either.

回答1:

I don't think that there is an out-of-the-box support for this, but it would be trivial to write a plugin that will show this information.

For example to show everything that's stored in the ApplicationState you could write the following plugin:

[Glimpse.Core.Extensibility.GlimpsePluginAttribute]
public class ApplicationStateGlimpsePlugin : IGlimpsePlugin
{
    public object GetData(HttpContextBase context)
    {
        var data = new List<object[]> { new[] { "Key", "Value" } };
        foreach (string key in context.Application.Keys)
        {
            data.Add(new object[] { key, context.Application[key] });
        }
        return data;
    }

    public void SetupInit()
    {
    }

    public string Name
    {
        get { return "ApplicationState"; }
    }
}

and then you get the desired result:

and to list everything that's stored into the cache:

[Glimpse.Core.Extensibility.GlimpsePluginAttribute]
public class ApplicationCacheGlimpsePlugin : IGlimpsePlugin
{
    public object GetData(HttpContextBase context)
    {
        var data = new List<object[]> { new[] { "Key", "Value" } };
        foreach (DictionaryEntry item in context.Cache)
        {
            data.Add(new object[] { item.Key, item.Value });
        }
        return data;
    }

    public void SetupInit()
    {
    }

    public string Name
    {
        get { return "ApplicationCache"; }
    }
}