Create Glimpse Plugin

Glimpse Structuremap PluginGlimpse is diagnostics and debugging tool similar to FireBug and Chrome Developer Tools, but Glimpse provides .NET ASP.NET platform specific details. Glimpse provides details into the context of .NET ASP.NET MVC ¾ and Web Forms execution, routing, urls, model binding, configuration and into referenced libraries and frameworks. Glimpse 1.x provides plugin model to create additional plugins for Inversion of Control containers ( Ninject, StructureMap, Autofac, etc. ), ORMs (NHibernate, EF, etc ), RavenDb, FluentSecurity, etc. for an available package listing check nuget and Glimpse Packages. Time to show how simple creating a Glimpse Plugin can be.

The Glimpse upgrade to 1.x plugins no longer inherit from IGlimpsePlugin but instead ITab, TabBaseorAspNet. IDocumentation interface has been introduced to provide a URI to documentation. To create your first Glimpse plugin simply inherit from TabeBase and IDocumentatoin then provide a custom implementation for GetData, Name and DocumentationUri as seen in a basic plugin shell based on Glimpse 1.2.x below. This example is based on Glimpse.Ninject.

  • ITab interface to implement Glimpse tabs.
  • TabBase simply selects a few common default configuration options for a tab.
  • AspNetTab does the same for tabs specifically targeting the ASP.NET runtime.
  • Name property is used as the label of the tab in the Glimpse client.
  • GetData method returns the data to be display inside of said tab.

Custom Runtime Policies is an advanced Glimpse feature permitting hardening or softening the rules Glimpse will process through IRuntimePolicy implementations.

Quick Steps

  • Create class library
  • Delete Class.cs file
  • Add reference to Glimpse.Core using NuGet Package Manager Console
    • Install-Package Glimpse
  • Create a new class, Glimpse{MyPluginName}Plugin
  • Inherit and implement from TabBase and IDocumentation similar to below
    • For GetData method return an empty collection
    • For Name property return name of your plugin
    • For DocumentationUri return url for help documentation, for now empty string is fine
  • Build
  • Create a dummpy ASP.NET MVC web site project
  • Add reference to Glimpse.Core, Glimpse.MVC3 or Glimpse.MVC4 using NuGet Package Manager Console
    • Install-Package Glimpse.MVC3
  • Add your Glimpse plugin as a reference via project ref
  • Build
  • F5
  • Open the Glimpse contol viewer to see your plugin as an empty tab
app.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
using System.Collections.Generic;
using System.Linq;
//
using Glimpse.Core.Extensibility;

namespace Glimpse.Ninject
{
    /// <summary>
  /// The ninject glimpse plugin.
  /// </summary>
  public class GlimpseNinjectPlugin : TabBase, IDocumentation
  {
      public override object GetData(ITabContext context)
      {
            var returnCollection = new Dictionary<string, object>();
          
          // .. Add logic to build collection for display in Glimpse Tab

            return returnCollection;
      }

      public override string Name
      {
            get { return "Ninject"; }
      }

      public string DocumentationUri
      {
            get { return ""; }
      }
  }
}

Now update the GetData method to capture the data for display in the Glimpse tab. In this example the other chose to us a Dictionary<string, object>, but a generic list is fine too, List<object[]>. Basically the modules are pulled from Ninject Container using Linq, stuffed into a dictionary using the module name as the key then added to the main dictionary where the key is the section name in the Glimpse tab ultimately displayed.

app.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
using System.Collections.Generic;
using System.Linq;
//
using Glimpse.Core.Extensibility;
//
using Ninject;
using Ninject.Modules;

namespace Glimpse.Ninject
{
    /// <summary>
  /// The ninject glimpse plugin.
  /// </summary>
  public class GlimpseNinjectPlugin : TabBase, IDocumentation
  {
      public override object GetData(ITabContext context)
      {
            var returnCollection = new Dictionary<string, object>();

            if (GetNinjectInstanceForGlimpseModule.CurrentKernel == null)
            {
                return "Unable to grab instance of the Ninject Kernel.";
            }

            IKernel kernel = GetNinjectInstanceForGlimpseModule.CurrentKernel;

            var modules =
                kernel.GetModules().OfType<NinjectModule>().Where(s => s.Name != "Ninject.Extensions.Glimpse.GetNinjectInstanceForGlimpseModule").
                    ToDictionary<NinjectModule, string, object>(module => module.Name, module => module.Bindings);

            returnCollection.Add("Modules & Bindings", modules);
            returnCollection.Add("Kernel Settings", kernel.Settings);

            return returnCollection;
      }

      public override string Name
      {
            get { return "Ninject"; }
      }

      public string DocumentationUri
      {
            get { return "https://github.com/segilbert/Ninject.Extensions.Glimpse/blob/glimpseupgrade/Readme.md"; }
      }
  }
}

References

Comments