Application and Page Level Tracing
Versions: 2008,2010
Published: 12/23/2010
Code: vstipProj0030
Work with SharePoint? Then make sure to check out the new SharePoint Developer Team Blog!
When debugging any project is is desirable to have as much information as possible to help deal with any issues. Working with web projects is always a challenge no matter what IDE you use. Fortunately, the great folks at the web team has provided a great tool just for web developers: tracing. Tracing can be enabled at two levels: application and page.
SECURITY WARNING: Generally, you should not enable tracing in an active Web site, because this can display sensitive configuration information to anyone who views pages in the Web site. Tracing is intended for debugging purposes only. If the localOnly attribute is true, trace information is displayed only for localhost requests. Additionally, if <deployment retail=true> is set in the Web.config file, tracing is disabled.
Application Level Tracing
You can enable application tracing by going to Web.config and adding the <trace> element inside <system.web>:
<configuration>
<system.web>
<trace enabled="true" requestLimit="50" localOnly="true" />
<system.web>
<configuration>
To access the application trace information, simply append "trace.axd" to the root of your web site:
This is a part of what you will see:
Attributes
There are several attributes that can be applied when using trace, all of which are optional:
enabled -- (boolean) specifies whether tracing is enabled for an application. Tracing must be enabled in order to use it. The default is false.
localOnly -- (boolean) indicates whether the trace information is available only on the host Web server. If false, the trace is visible from any computer which can be a security issue. The default is true.
mostRecent -- (boolean) shows whether the most recent application-level tracing output is displayed and older trace data beyond the limits that are indicated by the requestLimit is discarded. If false, trace data is displayed for requests until the requestLimit attribute is reached. The default is false.
pageOutput -- (boolean) specifies whether trace output is rendered at the end of each page. If false, trace output is accessible through the trace utility only. The default is false.
requestLimit -- (Int32) indicates the number of trace requests to store on the server. If the limit is reached and the mostRecent attribute is false, trace is automatically DISABLED. The maximum request limit is 10,000. If a value that is greater than 10,000 is specified, it is silently rounded down to 10,000 by ASP.NET. The default is 10.
traceMode -- the order in which to display trace information. The traceMode attribute can be one of two possible values:
SortByCategory -- trace information is displayed alphabetically by user-defined category.
SortByTime -- trace information is displayed in the order that the trace information is processed.
The default is SortByTime.
writeToDiagnosticsTrace -- (boolean) indicates whether ASP.NET Trace messages are forwarded to the System.Diagnostics tracing infrastructure, for any listeners that are registered to display Trace messages. The default value is false.
You can find more information here: https://msdn.microsoft.com/en-us/library/6915t83k.aspx
Trace Details
Additionally, you can dig into the details of each item and see a great deal of information:
Page Level Tracing
You may not want to have tracing enabled for the entire application for a variety or reasons such as performance, you want to focus in on just one page, etc. Turning on tracing for a page is very simple just turn on tracing in you @Page directive like this example:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication61._Default" Trace="true" %>
Now when you view that page it will show the trace information on the page:
This is equivalent to setting pageOutput to true at the application level but only impacts those pages where you have turned tracing on.
Combined Tracing
Tracing can get confusing but the one thing to remember is that the page always wins when tracing is explicitly set. Here is a table to help you keep the tracing results in mind if you set, both, application and page level tracing:
Application | Page | Result for a Page |
true | false | false |
false | true | true |
Finally
To say there is a lot going on here would be an understatement of epic proportions. So I will leave it to you to explore the details which can be found here:
https://msdn.microsoft.com/en-us/library/bb386420.aspx
Comments
- Anonymous
September 24, 2013
It's an excellent article for the beginner for the trace