Is there an option to write some logging information, like rendering name and data-source location, in every view programmatically, without go and modified in all views?
1 Answer
Yes, you can do it by the following way.
Configuration:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.renderRendering>
<processor type="YourAssembly.LogExecuteRenderer, YourAssembly" patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']" />
</mvc.renderRendering>
</pipelines>
</sitecore>
</configuration>
Code to render at the beginning of the rendering:
public class LogExecuteRenderer : ExecuteRenderer
{
protected override bool Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
{
writer.WriteLine($"<div renderingPath=\"{args.Rendering.RenderingItemPath}\" datasource=\"{args.Rendering.DataSource}\"></div>");
return base.Render(renderer, writer, args);
}
}
Code to render at the end of the rendering: (thanks for the comment @MarekMusielak)
public class LogExecuteRenderer : ExecuteRenderer
{
protected override bool Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
{
var baseResult = base.Render(renderer, writer, args);
writer.WriteLine($"<div renderingPath=\"{args.Rendering.RenderingItemPath}\" datasource=\"{args.Rendering.DataSource}\"></div>");
return baseResult;
}
}
-
2You open a
<div>
tag in your snippet and then close</code>
tag. You may want to change this ;) You may also want to write towriter
where the component ends: save result ofbase.Render
to a variable, write component end info to writer and return the result. Anyway, good answer.– Marek Musielak ♦Aug 13, 2019 at 13:38 -
1@MarekMusielak that's right, thx! :) I include the second option as well in my answer if you don't mind. Aug 13, 2019 at 14:21