Last active
December 21, 2023 11:28
-
-
Save cassidydotdk/5f059c61f39bd520103141e5f9c6d3e9 to your computer and use it in GitHub Desktop.
Debug helper for Sitecore Renderings - based on http://www.letsdositecore.com/rendering-debug-information-in-sitecore-mvc-views/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Web; | |
using Sitecore.Mvc.Pipelines.Response.RenderRendering; | |
namespace Website.Pipelines.RenderRendering | |
{ | |
public class PerformanceMeasurementProcessorBegin : RenderRenderingProcessor | |
{ | |
public override void Process(RenderRenderingArgs args) | |
{ | |
#if DEBUG | |
args.Writer.WriteLine(); | |
args.Writer.WriteLine("<!-- START: {0} -->", args.Rendering.Renderer); | |
var sw = new Stopwatch(); | |
HttpContext.Current.Items["PerformanceMeasurementProcessor:sw"] = sw; | |
sw.Start(); | |
#endif | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Web; | |
using Sitecore.Diagnostics; | |
using Sitecore.Mvc.Pipelines.Response.RenderRendering; | |
namespace Website.Pipelines.RenderRendering | |
{ | |
public class PerformanceMeasurementProcessorEnd : RenderRenderingProcessor | |
{ | |
public override void Process(RenderRenderingArgs args) | |
{ | |
#if DEBUG | |
var sw = HttpContext.Current.Items["PerformanceMeasurementProcessor:sw"] as Stopwatch; | |
if (sw == null) | |
{ | |
Log.Warn("Stopwatch not found!", this); | |
return; | |
} | |
sw.Stop(); | |
if (sw.ElapsedMilliseconds > 100) | |
Log.Warn($"PERFORMANCE ALERT: {args.Rendering.Renderer} Time taken: {sw.ElapsedMilliseconds}ms", this); | |
args.Writer.WriteLine(); | |
args.Writer.WriteLine($"<!-- END: {args.Rendering.Renderer} Time taken: {sw.ElapsedMilliseconds}ms -->"); | |
#endif | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<pipelines> | |
<mvc.renderRendering> | |
<processor type="Website.Pipelines.RenderRendering.PerformanceMeasurementProcessorBegin, Website" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"/> | |
<processor type="Website.Pipelines.RenderRendering.PerformanceMeasurementProcessorEnd, Website" patch:after="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"/> | |
</mvc.renderRendering> | |
</pipelines> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment