Error and Exception Handling in ASP.NET Application-Level Tracing (asp.net tutorial)

in #utopian-io6 years ago (edited)

Lesson Objectives

A. Working with Page Level Tracing
B. Working with Application Level Tracing
C. Logging Information and Windows Event Logs

Needs are;

A.Asp.NET
B. Visual Studio
C. C#

The difficulty level of commands we use is middle.

Application-Level Tracing

Application-Level Tracing allows tracing the information for all the web pages in an application. It provides an easy mechanism instead of enabling the trace for each and every page in the web application. Similarly if the Application-Level tracing is disabled, then the tracing is automatically disabled for all the web pages of the web application. The trace information is stored in the trace.axd file.The element inside the web.config file is used to configure application level tracing.

I. The element has various attributes such as "enabled", "localOnly",
"pageOutput", "requestLimit", and “traceMode".

II. The "enabled" attribute is set to "true" to enable application level tracing and "false" to
disable application level tracing.

III. The "localOnly" attribute is set to "true" to allow access of the trace.axd file on the server
only.

IV. The "pageOutput" attribute is set to true to display the trace information inside the web
page in the browser.

V. The "requestLimit" attribute is set to a number which represents the number of request
for which the trace information has to be stored.

VI. The "traceMode" attribute is set to "SortByCategory" or "SortByTime" to sort the trace
information as required.

The application level tracing can be configured in the web.config file by using the tags below:

< configuration >
< system.web >
< trace enabled="true"
requestLimit="20"
pageOutput="false"
localOnly="true" / >
< system.web >
< /configuration >

Demonstration: Implementing Application Level Tracing
Let us create a page and trace the information about all the pages in the application.

Steps to create the Demo:

1 ) Open the web.config file and add the following code below the < system.web > element.

< trace enabled="true"
requestLimit="20"
pageOutput="false"
localOnly="true" / >

2 ) Add a new Web Form named as “ApplicationTrace1.aspx” to the already created “ErrorAndExceptionHandlingDemo” web project.

3 ) Switch to the code-behind file and enter the following code below:

protectedvoid Page_Load(object sender, EventArgs e )
{ Trace.Write ( "Message from ApplicationTrace1 !" ) ;
}

4 ) Add a new Web Form named as “ApplicationTrace2.aspx” to the already created “ErrorAndExceptionHandlingDemo” web project.

5 ) Switch to the code-behind file and enter the following code below:

protectedvoid Page_Load ( object sender, EventArgs e )
{
Trace.Write ( "Message from ApplicationTrace2 !" ) ;
}

6 ) Right-Click the “ApplicationTrace1.aspx” file and select “Set as Start Page”.
7 ) Execute the project by pressing “F5” key.
8 ) View the Output 1 below:

9 ) The “url” in the browser displaying the Output 1 is
http://localhost:1043/ErrorAndExceptionHandlingDemo/ApplicationTrace1.aspx”

10 ) Remove the “ApplicationTrace1.aspx” from the above “url” and replace it with “trace.axd”.

11 ) The “url” should look as follows :
http://localhost:1043/ErrorAndExceptionHandlingDemo/Trace.axd”

12 ) Press Enter to load the “Trace.axd” page.

13 ) View the Output 2 below:

14 ) Click the “View Details” link in Output 2 window

15 ) View the Output 3 below :

16 ) View the output 3 above and find “Message from ApplicationTrace1” displayed in the trace.

17 ) The “url” in the browser displaying the Output 3 is
http://localhost:1043/ErrorAndExceptionHandlingDemo/Trace.axd?id=0”

18 ) Remove the “Trace.axd?id=0” from the above “url” and replace it with “ApplicationTrace2.aspx”.

19 ) The “url” should look as follows :
http://localhost:1043/ErrorAndExceptionHandlingDemo/ApplicationTrace2.aspx”

20 ) Press Enter to load the page.

21 ) View the Output 4 below:

22 ) The “url” in the browser displaying the Output 4 is
http://localhost:1043/ErrorAndExceptionHandlingDemo/ApplicationTrace2.aspx”

23 ) Remove the “ApplicationTrace2.aspx” from the above “url” and replace it with “trace.axd”.

24 ) The “url” should look as follows :
http://localhost:1043/ErrorAndExceptionHandlingDemo/Trace.axd”

25 ) Press Enter to load the “Trace.axd” page.

26 ) View the Output 5 below:

27 ) Click the bottom “View Details” link in Output 5 window

28 ) View the Output 6 below :

View the output 6 above and find “Message from ApplicationTrace2” displayed in the
trace.

29 ) Analyzing the above outputs it is clear that the trace for both the pages are stored in the
“trace.axd” file and it is application level tracing.

Application Level Error Handling
The Application Level Error handling provides a centralized location for handling the errors raised in a web application. The Errors can be handled at the application level by writing code in the Application_Error(object sender, EventArgs e) which is present in the Global.asax. When an exception occurs in a web page, the exception is caught in the web page. If the web page does not handle the exception, the error page specified in the web.config is displayed. If the web.config is not configured for handling exceptions, then the exception is sent to the Application_Error(object sender, EventArgs e).

Logging Information
The Exceptions that occur in a web application can be logged for diagnostic purposes.Information can be logged easily and effectively in asp.net by using the Windows Event Logs.The various event logs Application Log, Security Log, System Log, Setup Log and Forwarded Events. These logs are available in the Windows Operating System and can be accessed by all the web applications. The Windows log stores information about the logged message such as the Type, Date, Time, Source, Category, Event, User and Computer.

The Windows Event Logs
The windows Event Logs can be view using the Event Viewer. The uses of the various logs
are given below:
I. Application Log is used to store the errors which occur in the application.

II. Security Log is used to store the errors which are related to security in the application.

III. System Log is used to store the information about the various operating system events.

IV. Setup Log is used to store the information about the errors which occur during software installations on the Windows operating System.

Note : The Setup Log is not available in Windows XP. It is available in Windows vista and
Windows 7.

Exception Handling
Exceptions are the abnormal conditions which occur during the execution of a program. For example, user may enter a wrong input, or there may be a network failure. ASP.NET provides structured exception handling mechanism to handle exceptions. The .NET Framework provides numerous exception classes to handle exceptions easily. The Exception class is the base for all exceptions in .NET Framework. Some of the pre-defined exception classes commonly used to handle exceptions are ArithmeticException,NullReferenceException,IndexOutOfRangeException, OutOfMemoryException, and DivideByZeroException, etc.

  • The Tracing is a process which is used to collect the information of a particular web page for diagnostic purposes.

  • The element has various attributes such as "enabled", "localOnly", "pageOutput", "requestLimit", and “traceMode".

  • The Windows log stores information about the logged message such as the Type, Date, Time, Source, Category, Event, User and Computer.

documents required

note:



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @mrsx7 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.031
BTC 61906.25
ETH 2905.39
USDT 1.00
SBD 3.64