andrewlocatelliwoodcock

Thoughts on Software

LoggingViewEngine: a concrete example of using the Shaver framework

with 6 comments


As I explained in Shaver View Engine: a more in-depth look, Shaver is a framework for an extensible view engine that allows views to be written in Razor syntax, captures the HTML/CSS output using the Razor view engine and then makes that output available for use via extension points so that the original Razor view can be repurposed to produce any output you want or perform additional tasks prior to returning to the browser, or both.

If the output you want can be produced from HTML, or make use of HTML, Shaver facilitates this but does nothing with the output itself: that task is left up to concrete implementations of the View Engine. Shaver is an abstract View Engine and we need concrete implementations in order to do anything useful.

This post is going to concentrate on a simple concrete implementation of the Shaver View Engine, LoggingViewEngine. The LoggingViewEngine returns the view as normal but first logs the rendered HTML via Log4Net. And all that is required to achieve this functionality is to change the View’s extension to cslog …

LoggingViewEngine: Implementing the Shaver API

The LoggingViewEngine has two methods: a constructor, which we will discuss in a moment, and an implementation of ShaverViewEngine’s virtual method CreateReturnView. Remember that CreateReturnView is an extension point in the Shaver API: Shaver overrides Razor’s implementation of the standard CreateView method and uses it to create an IView object which is then passed, along with the ControllerContext to the virtual method CreateReturnView. Custom View Engines that implement the Shaver framework such as LoggingViewEngine can perform any custom tasks required within their own implementation of CreateReturnView: it is the output of CreateReturnView that is returned to MVC!

LoggingViewEngine: the constructor

The constructor is very simple and just tells MVC that this View engine wil deal with Views ending in the extension .cslog that are found in the folders “~/Views/{Controller}/{Action}.cslog”, “~/Views/Shared/{Action}.cslog”, where {Controller} is the Controller and {Action} is the Action. This step is required because Shaver cannot know which View Engines are interested in which Views.

LoggingViewEngine: CreateReturnView

The CreateReturnView method instantiates and returns a new instance of LoggingView and it is LoggingView that does the work of capturing and logging the HTML generated from the underlying Razor view.

LoggingView

LoggingView inherits from ShaverView and implements the Setup, ActOnHtml and RenderView methods. LoggingView also declares an instance of ILog in order to be able to log messages to the Log4Net framework:

        private static readonly ILog _log = LogManager.GetLogger(typeof(LoggingViewEngine));

Setup method

In the Setup method, LoggingView calls the XmlConfigurator.Configure() method and then saves the view instantiated by the Razor View Engine. XmlConfigurator.Configure() configures Log4Net for use. Note also though that Log4Net needs to be configured somewhere, preferably in the web.config file of the hosting web site.
An example of a minimal web.config section to configure Log4Net to support LoggingViewEngine:
<configuration>
  <configSections>
    <section name=”log4net”
       type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />
  </configSections>
  <log4net>
    <appender name=”LogFileAppender” type=”log4net.Appender.FileAppender”>
      <param name=”File” value=”C:\Users\Administrator\Documents\Visual Studio 2010\Logs\MvcView.log” />
      <param name=”AppendToFile” value=”true” />
      <layout type=”log4net.Layout.PatternLayout”>
        <param name=”Header” value=”[StartLog]” />
        <param name=”Footer” value=”[EndLog]” />
        <param name=”ConversionPattern” value=”%d [%t] %-5p %c %m%n” />
      </layout>
    </appender>
    <root>
      <level value=”DEBUG” />
      <appender-ref ref=”LogFileAppender” />
    </root>
  </log4net>
  .
  .
  .
</configuration>

ActOnHtml method

The ActOnHtml method simply calls the Debug method of the Log4Net instance and passes the HTML captured from the rendered Razor view.

RenderView method

The RenderView method simply calls RenderView on the View cached by the Setup method. As this View is the RazorView instantiated by the RazorViewEngine, in effect all we are doing here is rendering the View as if it had been called as a standard Razor view. But our custom view and view engine have captured the rendered HTML and logged it using Log4Net in the meantime: great for debugging and analysis purposes as this feature can be turned on and off simply by changing the View’s extension to .cslog …

Written by andrewlocatelliwoodcock

November 17, 2011 at 21:05

6 Responses

Subscribe to comments with RSS.

  1. […] So let’s have a look at the Shaver API: ShaverViewEngine and ShaverView, followed by some examples of concrete implementations of the Shaver Framework: LoggingViewEngine. […]

  2. … [Trackback]…

    […] There you will find more Infos: andrewlocatelliwoodcock.com/2011/11/17/loggingviewengine-a-concrete-example-of-using-the-shaver-framework/ […]…

    E-Bike Fuehrerschein

    November 18, 2011 at 19:44

  3. Definitely composed content material material , thankyou for data .

    Elektrofahrrad FAQ

    November 18, 2011 at 19:45

  4. Online Article……

    […]The information mentioned in the article are some of the best available […]……

    good to overtrain

    November 23, 2011 at 13:02

  5. Hey very nice site!! Man .. Beautiful .. Amazing .. I will bookmark your web site and take the feeds also…I am happy to find numerous useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

    Rasheeda Pfotenhauer

    December 19, 2011 at 21:40

  6. I do agree with all the ideas you have presented in your post. They are really convincing and will definitely work. Still, the posts are very short for novices. Could you please extend them a little from next time? Thanks for the post.

    Kip Luz

    December 20, 2011 at 10:16


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: