DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • AI Technology Is Drastically Disrupting the Background Screening Industry
  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • Simplifying SAP Data Integration With Google Cloud
  • Using DuckDB With CockroachDB
  1. DZone
  2. Coding
  3. Frameworks
  4. Page Instrumentation in ASP.NET 4.5

Page Instrumentation in ASP.NET 4.5

Imran Baloch user avatar by
Imran Baloch
·
Nov. 27, 13 · Interview
Like (0)
Save
Tweet
Share
4.77K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction:

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.

Description:

To start instrumenting a page, you need to inherit PageExecutionListener class, 

 public abstract class PageExecutionListener
    {
        protected PageExecutionListener();
        public abstract void BeginContext(PageExecutionContext context);
        public abstract void EndContext(PageExecutionContext context);
    }

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,

public class PageExecutionContext
    {
        public PageExecutionContext();
        public bool IsLiteral { get; set; }
        
        public int Length { get; set; }
       
        public int StartPosition { get; set; }
        
        public TextWriter TextWriter { get; set; }
        
        public string VirtualPath { get; set; }
    }

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),

 @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Home</title>
    </head>
    <body>
        <div>
        </div>
    </body>
    </html>

The Razor View Engine will emit the following C# code,

public class _Page_Views_Home_Index_cshtml : System.Web.Mvc.WebViewPage<dynamic>
    {
        public _Page_Views_Home_Index_cshtml()
        {
         }
         
         protected ASP.global_asax ApplicationInstance {
             get {
                 return ((ASP.global_asax)(Context.ApplicationInstance));
             }
         }
         public override void Execute()
         {
             
             Layout = null;
             
            #line default
            #line hidden
             BeginContext("~/Views/Home/Index.cshtml", 25, 48, true);
             WriteLiteral("\r\n\r\n<!DOCTYPE html>\r\n\r\n<html>\r\n<head>\r\n    <meta");
             EndContext("~/Views/Home/Index.cshtml", 25, 48, true);
             BeginContext("~/Views/Home/Index.cshtml", 73, 16, true);
             WriteLiteral(" name=\"viewport\"");
             EndContext("~/Views/Home/Index.cshtml", 73, 16, true);
             BeginContext("~/Views/Home/Index.cshtml", 89, 29, true);
             WriteLiteral(" content=\"width=device-width\"");
             EndContext("~/Views/Home/Index.cshtml", 89, 29, true);
             BeginContext("~/Views/Home/Index.cshtml", 118, 59, true);
             WriteLiteral(" />\r\n    <title>Home</title>\r\n</head>\r\n<body>\r\n    <div> \r\n");
             EndContext("~/Views/Home/Index.cshtml", 118, 59, true);
             BeginContext("~/Views/Home/Index.cshtml", 177, 8, true);
             WriteLiteral("        ");
             EndContext("~/Views/Home/Index.cshtml", 177, 8, true);
             BeginContext("~/Views/Home/Index.cshtml", 186, 2, false);

#line default
#line hidden
             EndContext("~/Views/Home/Index.cshtml", 186, 2, false);
             BeginContext("~/Views/Home/Index.cshtml", 188, 32, true);
             WriteLiteral("\r\n    </div>\r\n</body>\r\n</html>\r\n");
             EndContext("~/Views/Home/Index.cshtml", 188, 32, true);
         }
     }

Finally, here is a simple implementation of PageExecutionListener class which calculates the total time each time whenever something is written to the output response,

protected void Application_BeginRequest()
        {
            Context.PageInstrumentation.ExecutionListeners.Add(new MyPageExecutionListener());
        }
        public class MyPageExecutionListener : PageExecutionListener
        {
            Stopwatch s = new Stopwatch();
            public override void BeginContext(PageExecutionContext context)
            {
                s.Start();
            }
            public override void EndContext(PageExecutionContext context)
            {
                s.Stop();
                var e = string.Format("It takes {0}", s.Elapsed.ToString());
                Trace.WriteLine(e);
                s.Reset();
            }
        }

Summary:

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. Hopefully you enjoyed my this article too.

ASP.NET Instrumentation (computer programming)

Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • AI Technology Is Drastically Disrupting the Background Screening Industry
  • WireMock: The Ridiculously Easy Way (For Spring Microservices)
  • Simplifying SAP Data Integration With Google Cloud
  • Using DuckDB With CockroachDB

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: