Starting with release 4.1 we have add the ability to add basic diagnostics into Glass.Mapper to monitor the time it takes to create models and also the number of models requested.

Enabling

To enable diagnostics it must be first enabled. To do this we must set the Config.Debug.Enable property in the GlassMapperScCustom.CreateResolver() method. It is important that this occurs before we create the resolver:

		public static IDependencyResolver CreateResolver(){
			var config = new Glass.Mapper.Sc.Config();

		        config.Debug.Enabled = true;
		   
			var dependencyResolver = new DependencyResolver(config);
			// add any changes to the standard resolver here
			return dependencyResolver;
		}

By setting the flag to true then additional tasks will be added to the Glass.Mapper pipelines to start recording data. This will alter the number of tasks in certain pipelines, therefore if you are adding additional pipeline tasks using the following methods:

  • AbstractConfigFactory.Insert
  • AbstractConfigFactory.RemoveAt
  • AbstractConfigFactory.Replace

Should be replaced with the InsertBefore, InsertAfter, Remove and Replace<TReplace,TK$gt; methods to avoid tasks being in the wrong order when debugging.

Model Timer

The model timer allows you to understand how long it takes Glass to construct an object. When an item is requested Glass will start a timer for each model requested and then report in the logs any model that takes longer than the threshold limit.

The default limit is 100ms but can be altered by change the debug settings:

		public static IDependencyResolver CreateResolver(){
			var config = new Glass.Mapper.Sc.Config();

		    config.Debug.Enabled = true;

		    //set a new Threshold for the model timer:
		    config.Debug.SlowModelThreshold = 50;

			var dependencyResolver = new DependencyResolver(config);
			// add any changes to the standard resolver here
			return dependencyResolver;
		}

When the threshold is hit a log entry is written to the Sitecore log:

2668 20:39:22 WARN  Slow Glass Model - Time: 60 Cachable: False Type: Castle.Proxies.FeatureProxy Key: DefaultglassWebsite{9D4A5A92-C13B-4080-81CD-3FDB3708D3C7}4e01fae5-3985-4ff3-94fd-992499365ef5masterGlass.Demo.FeatureTruestopwatch

Construction Counter

The Construction Counter allows you to track how many items were created by a section of code. This can be achieved by using the class Glass.Mapper.Diagnostics.GlassModelCounter, you must tell the counter which SitecoreService you want to monitor and also an identifier. This identifier is written to the Sitecore log along with the number of models requested:

var sitecoreContext = SitecoreContext.GetFromHttpContext();

using(new Glass.Mapper.Diagnostics.GlassModelCounter(sitecoreContext, "Build Feature")){

   var home = sitecoreContext.GetCurrentItem<home>();
   var features = home.Features;
}

The counter has a threshold limit below which it won't report the number of models created. This can be useful if you are trying to find code that creates lots of models. The default threshold is zero but this can be increased by setting the third constructor parameter new Glass.Mapper.Diagnostics.GlassModelCounter(sitecoreContext, "BuildFeature", 10).

Once created the following entries will be written to the log:

7352 21:16:56 DEBUG Glass Counter Build Feature - Requests: 71 Cache Misses: 71

The Requests value is the number of models Requested from Glass.Mapper and Cache Misses is the number of models not returned from the Glass.Mapper cache.