Programmatically add log information in all renderings(controller, view) html 2e.li ssand70 100AalUuhm Lf i00 kPrepy Zogmschf
2
Is there any option to write some logging information like rendering name and data-source location in every view by programmatically, with out go and modified in all view.
presentation mvc pipelines
add a comment |
1 Answer
active
oldest
votes
8
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 6 hours ago -
1@MarekMusielak that's right, thx! :) I include the second option as well in my answer if you don't mind. – Tamás Tárnok 5 hours ago
add a comment |