December 27, 2013 11:03 by Robert
In this article, I showed you how to use Page Instrumentation, a new feature in ASP.NET 4.5 which might be useful for you during debugging.
ASP.NET 4.5 include a hidden gem called Page Instrumentation, very few people aware of this gem. You can use page instrumentation in ASP.NET 4.5 WebForm and MVC 5 or 4(assuming it targets 4.5). It allows you to inspect/instrument a web form or a mvc view during the rendering process. Page instrumentation are useful in scenarios when you have some performance issues regarding view engine rendering. In this article, I will show you how to use this feature in ASP.NET 4.5.
To start instrumenting a page, you need to inherit PageExecutionListener class.
The BeginContext will be called by a view engine before it renders the output for the specified context. Similarly, EndContext called by a view engine after it renders the output for the specified context. Both of these methods accept PageExecutionContext class as a parameter which have the following members.
All the properties of PageExecutionContext class are self explanatory. Let assume we have the following MVC view(for understanding how many times and when the BeginContext and EndContext will be invoke by the framework).
The Razor View Engine will emit the following C# code
Finally, here is a simple implementation of PageExecutionListener class which calculates the total time each time whenever something is written to the output response.