Web Design that taps into the haromny and vision of your dreams.

Tracing and Remote Debug in ASP.Net

How to remote debug an application

Written By on in C#

487 words, estimated reading time 3 minutes.

By default, ASP.Net will show a default "Server Error in Application" message when an unhandled exception occurs, something that is not very secure but useful for debugging. If you are working on a live box this is not a very secure way of working, it is much better to log the errors and handle the problem transparently to the user. This process is Tracing.

Active Server Pages (ASP.Net) Series
  1. What is ASP.Net?
  2. ASP.Net for PHP Developers
  3. Creating a Simple ASP.Net Page
  4. ASP.Net Website Navigation Using a SiteMap
  5. Using ASP.Net Master Pages and Content Pages
  6. Validating Input using ASP.Net
  7. Tracing and Remote Debug in ASP.Net
  8. Creating Custom Error Pages with ASP.Net
  9. Managing Session State in ASP.Net
  10. Using Themes and Skins in ASP.Net
  11. Creating User Controls in ASP.Net
  12. Difference between ASP.Net User and Custom Server Controls
  13. Creating Fully Themable Websites with ASP.Net
  14. Ultimate Guide to the Web.Config File
  15. Adding StyleSheets to ASP.Net via C#
  16. Extending the Web Sitemap Xml Document

Tracing and Remote Debug allows developers to access runtime information remotely using the trace object. This tutorial will cover setting up debug and tracing for a secure web server, using the trace object and viewing the results.

You may wish to create a custom error page for your website visitors. This will be shown instead of the default server error page. Developers can use remote debugging to analyse the problem.

During runtime, you can use the Debug and the Trace objects to output the value of variables, assert whether a condition is met, trace the execution path of the application and collect the information in a trace file.

Tracing can be configured on the page level or the application level.

Setting up Tracing

To enable page level tracing simply add Trace="true" to the page ASP directive:

<% page Language="c#" Trace="true" %>

Trace information will only be processed for the page(s) containing this flag.

Application level (every page) tracing can be enabled by enabling tracing within the web.config file located in the application root.

<trace enabled="true" pageOutput="true" localOnly="true" />

When enabled the rendered page will contain a great deal of debugging information such as response headers, the time taken to execute various stages of the request, session variables, server variables, session id's and so on. This is shown by default below the content of the page for local connections only.

You can also set the trace information to be stored within a file by setting pageOutput to false. This will store the trace information in a file located at http://server/project/trace.axd.

Trace and Debug in ASP.Net
Trace and Debug in ASP.Net

Using Trace

Writing Data to the Trace

Once trace is up and running you can write custom messages and variable dumps using the Trace object.

Trace.Write("category", "message");

Where category forms a section in the log which identifies a collection of messages and message is the text to show. You can use string.format to include variables.

Executing code only if tracing is enabled

There is a conditional that can be used to execute code if tracing is enabled, but skip over is it's not.

if (Trace.IsEnabled)
  string strMsg = "Tracing is enabled and working";
  Trace.Write("My Trace", strMsg);

Programmatically Enabling/Disabling Trace

Trace can be enabled or disabled at runtime by accessing the trace object and setting the IsEnabled value.

HttpContext.Current.Trace.IsEnabled = true;

Remote Debugging

Remote debugging enables web applications to be developed and debugged from outside the server. This allows for simplified team development and site management. Remote debugging requires that Visual Studio .Net or remote components be installed on the server. Visual Studio .Net must be used on the client. This is one of the few features that are not available on Express editions.

Last updated on: Friday 23rd June 2017



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me