Using Trace to speed up your website in C#.NET

Posted on: August 6th, 2011 by
Comments Requested

Before you deploy your .NET web application you must make sure that it’s performing up to par on speed. Web applications written in .NET, in particular, have a huge problem with speed when there is a lot of functionality on a specific page. This is not due to any technical limit of the .NET product. It is due to the fact that .NET gives you the tremendous freedom of being able to build a page dynamically from the code-behind side. Many technologies give you the ability to build a page dynamically from the client side. .NET, however, is unique in the consideration of the server-side dynamic pages. With this glorious freedom many programmers, including yours truly, coming from the programming side, as opposed to the web development side, go wild on creating complicated, functionality-loaded sites that could prove to be slow. For this purpose, .NET also provides a tool for speeding up your site specifically from the code-behind side. In this article I will be talking about how to use Trace to figure out which piece of code is giving you a hard time with respect to speed.

Of course, speeding up your client side is also a huge priority before deploying a site. There are many tools out there to help you speed up your site by making your client side more efficient; for example, making your javascript and html files more compact, statically and dynamically compressing your files, etc. Here are a few links to do so.


  1. SitePoint: Speed Up Your Site! 8 ASP.NET Performance Tips
  2. Best Practices for Speeding Up Your Web Site
  3. Let’s make the web faster
  4. 15+ Tips to Speed Up Your Website, and Optimize Your Code!
  5. 6 Ways to Speed Up Your Site
  6. Speed Up Web Site Load Time
  7. 25 Ways to Speed Up your Website
  8. Use Google Page Speed Tool to Speed Up Your Site

The trace module will tell you how long pre-render, render, etc. take place on your page and in addition it will display the time between any two points you specify in your code. You can hone in on the problamatic function, line of code, or database call that is causing you the problem by including more and more trace points within your code. Let’s see how this works.


There are two parts to making Trace work for you. First, you need to enable it in your Web.config file. And then, on each page that you want to inspect with trace you will need to make sure that you enable trace.


<system.web>
<trace enabled=”true” pageOutput=”true” localOnly=”false”
traceMode=”SortByTime” />
</system.web>



using System.Diagnostics;



Page.Trace.IsEnabled = true;
Page.Trace.TraceMode = TraceMode.SortByTime;
Page.Trace.Write(“Started Page Load”);


[yellow]

In the Web.config file you want to meake sure to set pageOutput to ‘true’ if you want the results of the trace to go straight onto the page you are attempting to trace. The trace results will then appear on the bottom of the page.

[red]

Even though your trace is enabled in the Web.config file, it might still be disabled on your individual pages by default. On any page that you want to include trace and custom trace messages, make sure that your trace is enabled and that you have the trace mode set to be sorted by time. It will be hard to see which parts of code are giving you trouble if you don’t see the respective times side by side.

[blue]

For each page that you want to include custom messages, make sure you include using the System.Diagnostics namespace.

Here’s how your trace output would look

Trace Debug


You can also make your trace’s output go to a specific file or xml instead of straight onto the page. I use the output to the page since it is the fastest. However, if your site is live and has lots of users, you might not want your viewers to see all the trace information on the bottom of the page. There is also much much more you can find out about your page with trace information. For more information on trace and on how to make your trace output go straight to a file, check out:

How to trace and debug in Visual C#


Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>