3

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?

2
  • What do you mean by "go and modified"? Commented Aug 13, 2019 at 21:45
  • @PeterMortensen manually edit all the cshtml files.
    – Andi AR
    Commented Aug 14, 2019 at 7:23

1 Answer 1

9

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;
    }
}
2
  • 2
    You open a <div> tag in your snippet and then close </code> tag. You may want to change this ;) You may also want to write to writer where the component ends: save result of base.Render to a variable, write component end info to writer and return the result. Anyway, good answer.
    – Marek Musielak
    Commented 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. Commented Aug 13, 2019 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.