Using Glimpse ASP Help

I use the custom tracing and logging approaches when I need to find a specific problem, but I use a tool called Glimpse when I don’t know where to start looking or when I need to look at the overall behavior of the application. Glimpse is an open source diagnostics package that builds on the ASP.NET logging and tracing features and adds a lot of useful insight into how requests are handled within an application. In the sections that follow, I’ll show you how to install and use Glimpse and explain why I find it so useful. Table 8-6 puts Glimpse into context.

Table 8-6. Putting Glimpse in Context

Installing Glimpse

The simplest way to install Glimpse is using NuGet. Enter the following command into the Visual Studio Package Manager Console:

When you hit Enter, NuGet will download and install the Glimpse packages that support MVC framework applications. The package I have installed is for MVC 5, but there are also packages for earlier MVC releases. Once the installation is complete, start the application and navigate to the /Glimpse. axd URL. You will see the Glimpse configuration page, as shown in Figure 8-4.

Tip Glimpse has a large catalog of extensions and packages that can be used to extend monitoring to other software components, including Entity Framework, and other parts of the ASP.NET technology stack, such as SignalR.

Click the Turn Glimpse On button to enable Glimpse diagnostics. The /glimpse. axd page is also used to configure Glimpse, but the default configuration is suitable for most projects, and I don’t need to make any configuration changes for this chapter.

Using Glimpse

Using Glimpse is as simple as requesting a URL from your application. Glimpse inserts a toolbar at the bottom of the screen that summarizes how the request has been handled, as shown in Figure 8-5.

Figure 8-5. The Glimpse toolbar

It can be hard to make out the details of the toolbar from Figure 8-5, so I have increased the scale and broken the bar into sections in Figure 8-6 so you can see the details.

Figure 8-6. A closer look at the Glimpse toolbar

The summary is broken down into three sections. The HTTP section reports on the total amount of time elapsed from the moment that the request was started until the moment when the response was rendered and displayed to the user. The HOST section provides information about how long the action targeted by the request took to execute and how much time it took for the view be rendered. The final section, AJAX, summarizes the Ajax requests made by the content received from the server.

You can get more detailed information by moving the mouse over a section. Figure 8-7 shows the details of the HOST section, which is the one that you will most often be interested in when working with the ASP.NET platform.

Figure 8-7. A more detailed view of the HOSTsection

Glimpse provides a lot more detail about requests when you click the G icon at the right edge of the toolbar. This opens a tabbed window that details every aspect of the request and how it was handled by the applications. There are tabs that detail the request, the configuration and environment of the server, the routes that matched the request and much more. One of the most useful tabs is called Timeline, and it provides performance information about the way that the request was processed, as illustrated by Figure 8-8.

Figure 8-8. The Glimpse Timeline tab

The Timeline tab shows the amount of time the request spent in the ASP.NET platform and the amount of time the MVC framework spent executing controllers, filters, and views. There is also an end-to-end view that shows the flow of the request through the different components of the application, which can help identify those actions or views that are taking too long to complete. I am only touching on the surface of the information provided by Glimpse, but you can see that there is a lot of detail available, and I recommend that you take the time to explore it fully.

Adding Trace Messages to Glimpse

One of the tabs in the Glimpse detail view is Trace, but it doesn’t capture the messages written through the HttpContext. Trace object unless you set writeToDiagnosticsTrace to true on the trace element in the Web. config file,as shown in Listing 8-7.

Listing 8-7. Forwarding Trace Messages So They Can Be Read by Glimpse

The additions to the Web. config file were added when Glimpse was installed.If you restart the application, open the Glimpse tabbed window, and move to the Trace tab,you will see the trace messages from the module, as shown in Figure 8-9.

Figure 8-9. Trace information displayed by Glimpse

Summary

In this chapter I showed you the facilities that ASP.NET provides for logging and tracing requests. I showed you how to handle the logRequest method in the request life cycle and explained the use of the TraceContext class, instances of which are available through the HttpContext. Trace property. I demonstrated the built-in ASP.NET support for viewing trace information and finished the chapter by introducing Glimpse, which is an excellent open source diagnostics tool. In Part 3, I show you how ASP.NET builds on the foundation of handling features that described in this part of the book to provide useful services to web application developers.