Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Advanced ASP.NET Trace Viewer

DZone's Guide to

Advanced ASP.NET Trace Viewer

In this article, you'll learn how to get to the bottom of code performance issues using ASP.NET tracing, and a free tool that helps make tracing easier.

· Performance Zone
Free Resource

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.

[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.

Image title

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.

Image title

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.

Image title

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.

Topics:
performance ,tracing ,asp.net

Published at DZone with permission of Matt Watson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}