We have an asp.net MVC web application which uses the HttpRuntime.Cache
object to keep some static lookup values. We want to be able to monitor what\'s
being cached
Use ASP.NET MVC itself.
public ActionResult Index()
{
return View(HttpRuntime.Cache)
}
and for the view
Html.DisplayForModel()
You will want to use a custom object template (basically take the MVC template and turn off the depth restriction).
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html
On the object template you will want to alter
else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
<%= ViewData.ModelMetadata.SimpleDisplayText %>
And change the > 1 to either be a higher number like 5-10 or just completely remove this depth check (I'd probably start with a 5 and go from there).
You could try to Json serialize it using the JavaScriptSerializer class. This way you don't need to cast to the original type as the Serialize
method can take any object and output it in a humanly readable JSON format. It might choke on some complex types and if this happens you may also try the Newtonsoft Json.NET.
Well, I think what you are asking for is a way of determining what the type parameters of a generic type is at runtime - in your example the situation is complicated because you are after an interface not an object instance.
Nethertheless this is still pretty straightforward, the following example should at least point you in the right direction on how to do this:
object obj= new List<string>();
Type type = obj.GetType();
Type enumerable = type.GetInterfaces().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
if (enumerable != null)
{
Type listType = enumerable.GetGenericArguments()[0];
if (listType == typeof(string))
{
IEnumerable<string> e = obj as IEnumerable<string>;
}
}
But I can't really see how this helps you solve your underlying problem of monitoring your cache?
In the past when attempting to monitor the performance of caches I've found creating my own simple Perfmon counters very helpful for monitor purposes - as a basic example start with a "# Entries" counter (which you increment whenever an item is added to the cache and decrement whenever an item is removed from the cache), and add counters as that you think would be useful as you go - a cache hit counter and a cache miss counter are normally pretty useful too.
You can also have your Perfmon counter break down caching information by having many instances of your counters, one for each type being cached (or in your case more likely the generic IEnumerable
type being cached) - just as the "Process" perfmon counter group has an instance for each process on your system, you would have an instance for each type in the cache (plus you should also add a "_Total" instance or similar).
Using Perfmon counters by recording operations on the cache allows you to monitor your cache performance in a fair amount of detail with very little runtime performance overhead.
It's important to highlight that the cache exists in the App Domain for the MVC web app so wouldn't be accessible externally, you'd need to either implement some in-app monitoring or inter-App Domain comms to enable an external app to request the cache data from your MVC App Domain.