Using Debugging Tools in ASP.NET

In the creation process, writing or drawing an outline plays a major role, because in case of a failure, you do not need to waste your time scratching your head. Instead, you can refer to the outline to solve the problem. In any kind of development, if the developer starts logging the steps involved, debugging or finding errors and fixing them becomes very systematic and easy.

In programming, logging of the process usually refers to the ability of an application to incorporate the use of debugging, code tracing, performance counters, and event logs. In this section, you'll learn to use the different debugging tools.

Visual Studio .NET Debugger

Visual Studio has always provided the developer with very powerful GUI debuggers, and Visual Studio .NET is no exception to this tradition. The debugger built into Visual Studio .NET is powerful and has a lot of new features compared to the debugger that was available with Visual Studio 6.0. Features that were previously available to the developers of Visual Basic only, like the Immediate window, are now common to all the .NET languages. So, irrespective of the language that is used to create a Web application, the debugging tools remain the same, thereby delivering a better developer experience. You can use the Visual Studio .NET debugger to debug applications in one of the following two ways:

By using the Debug menu
By manually attaching the debugger to a running application

The debugger provides many options that allow you to check for any errors by running through the code step by step, skipping a code routine, or placing a breakpoint. A breakpoint marks a point in the code at which the application halts and enters a mode called Break mode. The different options available in this mode help a developer to trace the source of an error in an application.

Attaching a Debugger

In addition to attaching a debugger to an application when you start it, you can attach a debugger while an application is already running. To do so, complete the following steps:

1. Open the application in Visual Studio .NET.
2. Select Debug ® Processes to open the Processes dialog box.
3. Ensure that the "Show system processes" option is selected.
4. In the Available Processes pane, all the processes are listed. Scroll through the list to select aspnet_wp.exe.
5. Click Attach to open the Attach To Process dialog box. From the available choices, select Common Language Runtime and Script.
6. Click OK and then click Close.

After you've attached the debugger to your running application, you can debug the application to trace the source of error.

You can attach a debugger to a running Web page only if it is running in Internet Explorer. If you are viewing the page within the IDE, you will not be able to attach a debugger with your page.

The ASP.NET Trace functionality:

In ASP.NET, the trace feature ensures that the programmers are able to log their applications by providing the means to monitor and examine program performance either during development or after deployment. ASP.NET allows tracing at two levels:

Page-level tracing
Application-level tracing

Trace capability of ASP.NET is declared in the TraceContext class. To enable the Trace Page, the Trace directive must be set to True. The Trace property is exposed as a public property within ASP.NET pages. The Trace property refers to the TraceContext of the ASP.NET page.

The TraceContext class is a noninheritable public class of the .NET Framework that is used for capturing the execution details of a Web request and presenting the data on the page. For developers to include messages for specific Trace categories, this class can be a good utility. You need to include the System.Web namespace in your Web project to be able to use this class.
The class has certain properties and methods that provide the functionality for enabling
the debugging of the ASP.NET pages.

Page-level tracing

ASP.NET makes it easy to debug and test applications by providing a trace capability.

After the trace capability is enabled, ASP.NET provides the following functionalities automatically:

Creates and appends a table called the table of performance data to the end of the ASP.NET page.

Allows a developer to add custom diagnostic messages in the code wherever required.
Basically, the following are the two ways to generate trace statements in a page:

Use the code written within a file.
Use an HTML editor.
While generating the trace statements, you can include custom trace messages to the Trace log. Then, with the help of an HTML editor, you can present those messages and other trace information in a better manner.

You'll now write an ASP.NET page that generates the trace statements. Both Visual Studio .NET and Notepad can be used for writing the code. In this case, Notepad is used to create the ASPX file.

Application-level tracing

Application-level Tracing is enabled by using the Web.config file. This file is also used to enable the ASP.NET framework to gather the HTTP request information for the entire application.

Unlike page-level Trace statements, application-level Tracing does not present the Trace
information in a browser unless specified, but the information can be displayed in a Webbased
Trace viewer application. The Trace viewer displays trace information for a sequence of requests to the application, thus making it mandatory to store the matrix for each request in memory until tracing is enabled. This can be done by including a TraceContext class that participates in the HTTP execution of each request for the application.

Writing Good ASP.NET Code

Good programming practices make it easy to understand, debug, and maintain code. Poorly written programs lead to confusion while debugging. The developer who has written the code might find himself in a situation where he cannot tell what the code is doing. Therefore, every developer should follow good programming practices. The same applies for an ASP.NET developer. While developing an ASP.NET application, a developer must keep in mind certain guidelines with respect to form designing, naming variables and objects, designing and implementing programming logic, and using coding style.

Form design The form design should be visually appealing. It should be simple and neat. Although Web pages use bright colors and lots of images to make them attractive, this type of
design tak es a longer time to load the pages. Therefore, while designing pages, you should keep the following guidelines in mind:

The controls that need user input should have the correct tab order and should be grouped and arranged in an order that makes sense while entering data.
The controls should be properly aligned.
Variables and objects

While naming variables and objects, keep the following guidelines in mind:

Use a proper naming notation, such as Hungarian or camel-casing notation, to name variables and objects. Hungarian notation enables you to identify the datatype of the variable from the name of the variable. So, a variable storing the first name of an employee will be declared as
sFirstName. In camel-casing notation, the variable names take the form sfirstName, with the second part of the variable, which is a noun, capitalized.

Name the variables and objects meaningfully. Meaningful names combined with Hungarian notation make the purpose and type of the variables clear. This results in a self-documented code, which is easy to understand and maintain.

Declare the variables and objects in the beginning of a procedure.

Declaration in the beginning makes the code execution more efficient, besides making it easy to understand by someone looking at the code text.

Always initialize variables to certain default values before using them, to avoid any type conversion issues.

Always rely on explicit conversion functions to eliminate confusion.

Programming logic

While implementing the programming logic, you should do a good chunking of the code.
The chunking helps you to maintain the code and speed up debugging. Keep the following guidelines in mind:

If you want to implement a programming logic that returns a single result, use a function.
If you need multiple arguments to be passed without expecting a return value, use a procedure.

If you want to create a reusable piece of code, use functions or Sub procedures or put the code in a separate class (if the code can be logically grouped).

Coding style

The program should be easy to read and to understand when you need to refer back to it. Follow these guidelines while coding:

Always use "Option Explicit" to catch any undeclared or misspelled variables. Also, the use of "Option Explicit" makes the Web pages run fast. The "Option Explicit" option forces the explicit declaration of variables.

Declare one variable per line. This avoids confusion about datatypes.

Use comments wherever possible to document a difficult code section.

Use blank lines in the code for clarity.

Use proper code block indenting.

related post

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment