Debug ASP.Net MVC Application With Glimpse

Introduction

For debugging and diagnostic information for ASP.NET apps, Glimpse is a NuGet package that provides detailed performance. It is fast, super-light and serve the developer with the best visual display for all performance matrices. It helps the developer to debug the performance of ASP.Net applications through the data access layer to the presentation layer, debugging your Ajax call is an additional feature of this extension. You don't need to read several manual pages to use this extension compared to Fiddler :). The best part of Glimpse is to debug and analyze the performance of all the server-side functionality as we do for the client side where we have the Firebug, Fiddler and F-12 development tools.

Using the code

For this tutorial I will use a sample Contoso University Project available at MSDN.

1. Open ContosoUniversity.sln in Visual Studio, it is implemented using ASP.Net MVC5 and Entity Framework 6.

2. Click on Manage NuGet Packages in the Project Tab.

3 (a). NuGet will look for your project configuration and will recommend available Glimpse extensions, type Glimpse in the search box.

3(b). You may perform the same function using the NuGet Package Manager Console:

  • Console Command For MVC5: Install-Package Glimpse.MVC5
  • Console Command For MVC4: Install-Package Glimpse.MVC4
  • Console Command For MVC3: Install-Package Glimpse.MVC3
  • Console Command For WebForms: Install-Package Glimpse.Webforms 

 

  • Console Command For ADO.NET: Install-Package Glimpse.Ado
  • Console Command For Entity Framework 6: Install-Package Glimpse.EF6
  • Console Command For Entity Framework 5: Install-Package Glimpse.EF5
  • Console Command For Entity Framework 4 or 3: Install-Package Glimpse.EF43 


If you are installing the first time then it will automatically include Glimpse.AspNet and Glimspe.Core to resolve the dependency.

4. The following glimpse references will be added to the project:


And web.config will be updated as below:

  1. <configuration>  
  2.    <configSections>  
  3.       <section name="glimpse" type="Glimpse.Core.Configuration.Section, Glimpse.Core" />  
  4.    </configSections>  
  5.    <system.web>  
  6.       <httpModules>  
  7.          <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" />  
  8.       </httpModules>  
  9.       <httpHandlers>  
  10.          <add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" />  
  11.       </httpHandlers>  
  12.    </system.web>  
  13.    <system.webServer>  
  14.       <modules>  
  15.          <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" />  
  16.       </modules><handlers>  
  17.          <add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />  
  18.       </handlers>  
  19.    </system.webServer>  
  20.    <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">  
  21.    </glimpse>  
  22. </configuration>  

5. Build the project and open view the Home/About in a browser.

6. Type /Glimpse.axd after the available URL of your project.

7. Click on Turn Glimpse On to debug your application for better performance.

8. Glimpse will be available at the bottom-right of your browser.

9. When you hover on the HOST section of the dashboard, it will show all the trivial data about the total action time, number of db connections opened, number of queries served by the controller to render this view and much more.

10. Click on the 'g' at the bottom right of the toolbar and explore all the server-side debugging features. Using Glimpse you may go through cache entries.

11. Glimpse will help you out to analyze the performance of the controller methods by displaying the time elapsed for each method of the controller.

12. This extension will help the developer to display all the key-values of the cookie, the parameters of the query string and the attributes of the HTTP header.

13. The best part of Glimpse is to display the duration of each SQL query under the SQL tab.

14. Then comes timeline analysis of the execution of each event, this will help the developer to look for the area of concern in a more prominent way.

Points of Interest

There might be a chance you are not using Entity Framework in your project. Then for ADO.NET, to retrieve the data for the SQL tab, Glimpse attaches itself by providing its own GlimpseDbProviderFactory that wraps the standard ones. This lets it collect information about how you're using your data connections. If you're not using an ADO connection that uses the DbProviderFactories, glimpse won't collect any data for the SQL tab.

  1. DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");  
  2. using (var connection = factory.CreateConnection())  
  3. {  
  4.    using (var command = factory.CreateCommand())  
  5.    {  
  6.       try  
  7.       {  
  8.          connection.ConnectionString = connString;  
  9.          command.Connection = connection;  
  10.          command.CommandType = CommandType.StoredProcedure;  
  11.          command.CommandText = "StoreProcedureName";  
  12.   
  13.          var parameters = new[]{  
  14.          new SqlParameter(){ ParameterName="@param1", Value = param1 }  
  15.       };  
  16.       command.Parameters.AddRange(parameters);  
  17.       command.CommandTimeout = ConnectionTimeOut;  
  18.       connection.Open();  
  19.   
  20.       using (var myReader = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateDataAdapter())  
  21.       {  
  22.          //code here  
  23.       }  
  24.   
  25.    }  
  26.    finally  
  27.    {  
  28.       if (command.Connection.State == ConnectionState.Open)  
  29.       {
  30.          command.Connection.Close();  
  31.       }  
  32.    }  
  33. }   
Code available @Github.
 
Happy Debugging