Tuesday, April 18, 2017

Tracing in asp .net Application for Beginners

Today we will learn how to use Tracing in Asp.Net to check the performance of Web Application.
In Web Development the most concerned part is Performance. so how we can check which part of our application is slower either its Asp.Net code or our Database queries. you can check your SQL queries performance by using SQL Profiler, look the execution plan which part of query is taking more time than normal and then you can easily find alternative or some good way to resolve query slowness.
But if query is performing fast in database then it means there is something in our application which is making causing slowness. In production environment its not easy to check which line of code is making problem so by using Tracing we can easily check where is the problem actually. We can turn on Tracing by setting page directive Trace="true". once its set on page then we can check at the end of page or in trace.axd file. from trace log in the file we can easily check which piece of code is making problem.
Let's have look on code:
First in page directive you need to set:
<%@ Page Language="C#" AutoEventWireup="true" Trace="true" CodeFile="WebPage.aspx.cs" Inherits="WebPage" %>
Now we will create three Gridviews to check each gridview data load performance on Page load.
<div>
            <b>All Orders</b>
            <asp:GridView ID="GridView1" autogeneratecolumns="True" runat="server"></asp:GridView>    
            <br />
            <b>Delivered Order</b>
            <asp:GridView ID="GridView2" autogeneratecolumns="True" runat="server"></asp:GridView>    
            <br />
            <b>Cancelled Order</b>
            <asp:GridView ID="GridView3" autogeneratecolumns="True" runat="server"></asp:GridView>    
        </div>


And In code behind we will call our Stored Procedure on Page load, here i am not going to write full code for calling stored procedures, we will just understand how Tracing will work:
protected void Page_Load(object sender, EventArgs e)
    {
        Trace.Warn("GetAllOrders() Stored procedure started");
        GetAllOrder();
        Trace.Warn("GetAllOrders() Stored procedure End");

        Trace.Warn("GetDeliveredOrder() Stored procedure started");
        GetDeliveredOrder();
        Trace.Warn("GetDeliveredOrder() Stored procedure End");

        Trace.Warn("GetCancelledOrder() Stored procedure started");
        GetCancelledOrder();
        Trace.Warn("GetCancelledOrder() Stored procedure End");

    }


Above we discussed it was page level Tracing, We can also set Tracing on application level by using below web.config code. it save trace data for up to 40 requests.
<configuration>
  <system.web>
    <trace enabled="true" requestLimit="40" localOnly="false" />
  </system.web>
</configuration>


Comments and Suggestions are always Welcome!

No comments:

Post a Comment