Paste2 Logo
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Reflection;
  7. using EPiServer.PlugIn;
  8. using System.ComponentModel;
  9.  
  10. namespace EPiServer.Connectors
  11. {
  12.     [GuiPlugIn(
  13.     Area = PlugInArea.ReportMenu,
  14.     Description = "Page Event Report",
  15.     Category = "Events", DisplayName = "Page Events",
  16.     Url = "~/Connectors/EventReport.aspx")]
  17.     public partial class EventReport : EPiServer.UI.SystemPageBase
  18.     {
  19.         protected global::System.Web.UI.WebControls.Repeater Outer;
  20.         object _events = null;
  21.         #region EventKeys
  22.         static Dictionary<string, object> eventKeys;
  23.         public static Dictionary<string, object> EventKeys
  24.         {
  25.             get
  26.             {
  27.                 if (eventKeys == null)
  28.                 {
  29.                     Dictionary<string, object> tmp = new Dictionary<string, object>();
  30.                     Type t = EPiServer.DataFactory.Instance.GetType();
  31.                     FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
  32.                     foreach (FieldInfo field in fields)
  33.                     {
  34.                         if (!field.Name.StartsWith("_"))
  35.                         {
  36.                             object keyObject = EventReport.GetPrivateField(field.Name, EPiServer.DataFactory.Instance);
  37.                             if (!tmp.ContainsValue(keyObject))
  38.                                 tmp.Add(field.Name, keyObject);
  39.                         }
  40.                     }
  41.                     eventKeys = tmp;
  42.                 }
  43.                 return eventKeys;
  44.  
  45.             }
  46.         }
  47.         #endregion
  48.         protected void Page_Load(object sender, EventArgs e)
  49.         {
  50.  
  51.             _events = GetPrivateField("_events", EPiServer.DataFactory.Instance);
  52.             //Type t = EPiServer.DataFactory.Instance.GetType();
  53.             //FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
  54.             Outer.DataSource = EventKeys.Keys;
  55.             Outer.DataBind();
  56.             //Inner.DataBind();
  57.         }
  58.         public static object GetPrivateField(string key, object o)
  59.         {
  60.             Type t = o.GetType();
  61.             FieldInfo f = t.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
  62.             if (f == null)
  63.             {
  64.                 f = t.BaseType.GetField(key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
  65.             }
  66.             return f.GetValue(o);
  67.         }
  68.         #region Events
  69.         static EventHandlerList events;
  70.         public static EventHandlerList Events
  71.         {
  72.             get
  73.             {
  74.                 if (events == null)
  75.                 {
  76.                     events = (EventHandlerList)EventReport.GetPrivateField("_events", EPiServer.DataFactory.Instance);
  77.                 }
  78.                 return events;
  79.             }
  80.         }
  81.         #endregion
  82.         #region GetEvents
  83.  
  84.         public System.Delegate[] GetEvents(string key)
  85.         {
  86.             if (!EventKeys.ContainsKey(key))
  87.                 return null;
  88.  
  89.             object keyObject = EventKeys[key];
  90.             Type type = Events.GetType();
  91.             MethodInfo method = type.GetMethod("Find", BindingFlags.Instance | BindingFlags.NonPublic);
  92.             List<object> args = new List<object>();
  93.             args.Add(keyObject);
  94.             object eventStore = method.Invoke(Events, args.ToArray());
  95.             if (eventStore == null) return null;
  96.             object handler = GetPrivateField("handler", eventStore);
  97.             System.MulticastDelegate originalDelegate = (System.MulticastDelegate)handler;
  98.             System.Delegate[] result = originalDelegate.GetInvocationList();
  99.  
  100.             return result;
  101.         }
  102.         #endregion
  103.         public int GetCount(string key)
  104.         {
  105.             if (EventReportStartUp.CountList.ContainsKey(key))
  106.                 return EventReportStartUp.CountList[key].GetCount;
  107.             return -1;
  108.         }
  109.         #region OnPreInit
  110.         protected override void OnPreInit(EventArgs e)
  111.         {
  112.             base.OnPreInit(e);
  113.             this.MasterPageFile = ResolveUrlFromUI("MasterPages/EPiServerUI.master");
  114.         }
  115.         #endregion
  116.     }
  117.     public class EventReportStartUp : EPiServer.PlugIn.PlugInAttribute
  118.     {
  119.         public static Dictionary<string, EventReportCount> CountList = new Dictionary<string, EventReportCount>();
  120.         public static void Start()
  121.         {
  122.             Type t = EPiServer.DataFactory.Instance.GetType();
  123.             FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
  124.             MethodInfo method = EventReport.Events.GetType().GetMethod("AddHandler", BindingFlags.Instance | BindingFlags.NonPublic);
  125.             foreach (string key in EventReport.EventKeys.Keys)
  126.             {
  127.                 if (!key.Contains("Children"))
  128.                 {
  129.                     EventReportCount item = new EventReportCount();
  130.                     PageEventHandler handler = new PageEventHandler(item.Count);
  131.                     EventReport.Events.AddHandler(EventReport.EventKeys[key], handler);
  132.                     CountList.Add(key, item);
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     public class EventReportCount
  138.     {
  139.         int count = 0;
  140.         object lockObject = new object();
  141.         public int GetCount
  142.         {
  143.             get { return count; }
  144.         }
  145.         public void Count(object sender, EventArgs e)
  146.         {
  147.             lock (lockObject)
  148.             {
  149.                 count++;
  150.             }
  151.         }
  152.     }
  153. }
  154.