List of top features and tips for ASP.NET

Advanced ASP.NET Trace Viewer – WebForms, MVC, Web API, WCF

Matt Watson Developer Tips, Tricks & Resources, Stackify Product & Company Updates Leave a Comment

What did my code just do? That is a critical question that developers always need to know. Application logging, tracing, and profiling are the primary ways that developers can answer this question. In this article, we’ll review ASP.NET tracing and how to view your tracing statements with Prefix.

Intro to ASP.NET Tracing

Tracing is built into the .NET framework and has been available for years. Microsoft describes ASP.NET tracing as a way to view diagnostic information about a single request. It lets you see the page’s execution path, web request details, and much more.

Configuration

To enable ASP.NET tracing, you need to modify your web.config as shown below.

<configuration>
  <system.web>
    <trace enabled="true" requestLimit="40" localOnly="true" />
  </system.web>
</configuration>

If you are using MVC or Web API you also need to configure the WebPageTraceListener.

<system.diagnostics>
  <trace>
    <listeners>
      <add name="WebPageTraceListener"
            type="System.Web.WebPageTraceListener, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </listeners>
  </trace>
</system.diagnostics>

How to Write Traces

By default, ASP.NET collects a lot of details like cookies, headers, response status code and more. These basics can be very helpful, but being able to see your own tracing statements is even more valuable. You can log them as multiple tracing levels. Including warnings, information, error, and just trace.

You can easily write your own tracing statements as shown in this example.


Free Download

[System.Web.Http.HttpGet]
[System.Web.Http.ActionName("KitchenAsync")]
public async Task<HttpResponseMessage> KitchenAsync()
{
	Trace.WriteLine("Starting KitchenAsync - Call redis");
	await SERedisAsync();
	Trace.WriteLine("Call DB");
	await DBTestAsync();
	Trace.WriteLine("Call Webclient");
	await WebClientAsync();
	Trace.WriteLine("Finished KitchenAsync");
	return Request.CreateResponse(HttpStatusCode.OK, Guid.NewGuid().ToString());
}

Viewing ASP.NET Traces

Built into ASP.NET is a trace viewer. You can access it via your web browser by going to /trace.axd within your application.

This will load a list of the most recent web requests.

View ASP.NET Traces via Trace

View ASP.NET Traces via Trace

By clicking “View Details” you can view what is captured for a specific web request. Here is an example of the one from the C# code above. You can see the Trace.WriteLine messages that were logged.

ASP. NET Trace Detail

ASP. NET Trace Detail

ASP.NET Trace Viewer With Prefix

The built in view for ASP.NET tracing is pretty cool. However, if you want the ultimate experience, you want Prefix.

What is Prefix?

Prefix is a free developer tool from Stackify. It is installed on your workstation and runs as a lightweight ASP.NET profiler. It tracks key methods being called by your code and provides an excellent user interface to view .NET traces, logs, SQL queries, exceptions, and much more.

Prefix automatically tracks hundreds of key methods across dozens of common frameworks and dependencies. Things like SQL Server, mongodb, redis, etc. To view a complete list of what Prefix tracks, please visit our docs.

View ASP.NET Traces

Prefix collects a lot of similar details as the standard ASP.NET trace view. It also includes way more details. Not only does it automatically pick up the Trace.WriteLine statements, it also supports Debug.WriteLine and logging statements via popular logging frameworks like log4net, NLog, etc.

You can also see how the code interacts with Redis, 2 SQL queries, and an external HTTP web service call.

ASP.NET Trace View With Prefix

ASP.NET Trace View With Prefix

Summary

Understanding what your code is doing is essential to validate that your code works. Tools like Prefix help answer that critical question of “what did my code just do?”. Prefix gives you instant visibility to what your code is doing, way beyond just basic ASP.NET tracing.

Prefix is an amazing tool that is free to download! It shows off the power of lightweight ASP.NET profiling. You can get this same type of functionality with Stackify Retrace for your servers.

About Matt Watson

Matt is the Founder & CEO of Stackify. He has been a developer/hacker for over 15 years and loves solving hard problems with code. While working in IT management he realized how much of his time was wasted trying to put out production fires without the right tools. He founded Stackify in 2012 to create an easy to use set of tools for developers.