Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Performing ASP.

NET Administrative Tasks

Objective

Configuration as ASP.NET Application Debugging ASP.NET Application Identifying ASP.NET Application Debugging Methods Handling Internet Related Exceptions

ASP.NET Configuration System


ASP.NET Configuration system is used to describe the properties and behaviors of various aspects of ASP.NET applications ASP.NET uses XML-based configuration system that is more accessible and easier to use. You can configure features, such as Connection Strings, Authentication Modes, Caching, Debug and Tracing, Custom Errors and many more.

Configuration Files
ASP.NET configuration data is stored in two primary XML-based files. These files allow you to easily edit configuration data at any moment even after the application is deployed on server.

Different types of Configuration files

Machine.config: Server or machine-wide configuration file Web.config: Application configuration files which deal with a single application

Server Configuration file (Machine.config)

Every ASP.NET server installation includes a configuration file named machine.config, and this file is installed as a part of .NET Framework installation. You can find machine.config in C:\<Windows>\Microsoft.NET\Framework\\Config\

Application Configuration file (Web.config)

Each and Every ASP.NET application has its own copy of configuration settings stored in a file called Web.config. If the web application spans multiple folders, each sub folder has its own Web.config file that inherits or overrides the parent's file settings.

Common Configuration Settings (Machine.config & Web.config)

Common section groups/settings present in the configuration files.


Connection Strings Session State Custom Errors Authentication

Connection Strings
ASP.NET 2.0 introduces a new section called <connectionStrings> that stores all kinds of connection-string information. <configuration> <connectionStrings> <add name ="CookieDemo" connectionString ="server=aras02;database=aras02_Db; uid=freelance91;pwd=freelance91"/> </connectionStrings> </configuration/>

Session State

Session information using the <sessionState> element It can be used to persist state in any permanent store like XML or databases
sessionState mode ="StateServer" cookieless ="false" timeout ="20" stateConnectionString="tcpip=aras02:42424" stateNetworkTimeout="60" sqlConnectionString ="" />

Custom Errors
When the ASP.NET application fails, the ASP.NET page can show the default error page with the source code and line number. We can prevent this kind of error messages by configuring <customErrors> element which allows for defining custom error messages in an ASP.NET application. <customErrors mode="[on/off/RemoteOnly]" defaultRedirect="[URL]"> <error statusCode="[statusCode]" redirect="[URL]" />

Custom Application Specific settings


Every web application must store some applicationspecific information for its runtime use. The "appSettings" section provides a way to define custom application settings for an ASP.NET application.
<appSettings> <add key="[key]" value ="[Value]"/> </appSettings >

Debugging An Application
Debugging is the process of finding and fixing errors in your application Two types of errors:
Compile time errors: these are largely syntactic errors which will be captured by the compiler. Note, that use of Option Explicit and Option Strict can reduce the likelihood of runtime errors via compile time identification of likely issues. See my article on Error Handling in ASP.NET for more information. Runtime errors: these are bugs programs that compile successfully but do not behave as expected.

Debug Mode
Debug mode may be set explicitly within the web.config file
<compilation debug="true" />

Setting Breakpoints and Stepping Through Program Execution

Stepping is the step-by-step execution of a program The steps of the stepping process are definable by the developer. The debug menu of VS.NET provides three options for step execution: Step Into: executes the code in step mode if a method call is encountered the program execution steps into the code in step mode. Step Over: will not execute the next method call in step mode, continuing to the next statement after the method call. Step Out: if already in a method call will continue through the rest of the method without stepping, returning to step mode when control returns to the calling statement.

Watch

.NET debugging tools

The Watch window allows you to add 'watches' to particularly variables and expression for continuous monitoring. Thus when you set up a watch whenever you return to the IDE while debugging you will see the current value of the variable or expression defined by the watch. Autos The autos window (automatically) displays the variables in the current statement and the previous statement. Locals Displays the variables local to the current context, i.e. the current method under execution Me The Me window allows you to examine members associated with the current object typically a web form for an ASP.NET application, but not necessarily. It also allows you to change these values. Immediate The immediate window allows (immediate) access to the values of variables and expressions while debugging, at the breakpoint. Call Stack The Call Stack window shows you the method call stack thus providing information about the path taken by the code to reach its current point of execution

Enabling Debugging at the Application Level

Turning debugging on and off at the page level is easy enough when you're making changes to only a few pages You can activate Debug mode at the application level by setting the Debug attribute of the compilation section in Web.config
<configuration> <system.web> <customErrors mode="Off" /> <compilation defaultLanguage="C#" debug="true" numRecompilesBeforeAppRestart="15"> </compilation> </system.web> </configuration>

Using the Debug Object


The .NET framework provides a Debug object that you can use to assist with debugging your application The Debug object is a member of System.Diagnostics.Debug. private void Page_Load(Object Sender , EventArgs e)
{ Debug.Write("Application initializing. Poot."); }

Creating Custom Performance Monitors


A performance monitor is a feature of the Windows NT/2000 operating system used to determine operating system resource consumption in real-time ASP.NET have their own set of performance monitors that broadcast meaningful information to the operating system about resources consumed by these services .NET application can be equipped with performance monitors that you create yourself, called custom performance monitors

Windows Event Log


Windows event log is a central, system-managed place where any application, service, or device can log information. ASP.NET applications, can access the event log. Your applications will write information to the application log The .NET framework provides a class for handling the event log. This is the EventLog class, found in System.Diagnostics.

Event to the Windows Event Log


void Button1_Click(Object Sender, EventArgs e) { if(!EventLog.SourceExists("MyApp")) { EventLog.CreateEventSource("MyApp", "Application"); } EventLog.WriteEntry("MyApp", "This is just a test.", EventLogEntryType.Information); }

Tracing
Tracing is a way to monitor the execution of your ASP.NET application. You can record exception details and program flow in a way that doesn't affect the program's output.

Page level Tracing


ASP.NET tracing can be enabled on a page-by-page basis by adding "Trace=true" to the Page directive in any ASP.NET page
ASP.NET tracing can be enabled on a page-by-page basis by adding "Trace=true" to the Page directive in any ASP.NET page you can add the TraceMode attribute that sets SortByCategory or the default, SortByTime

Application Tracing

You can enable tracing for the entire application by adding tracing settings in web.config.
<configuration> <appSettings/> <connectionStrings/> <system.web> <compilation debug="false" /> <authentication mode="Windows" /> <trace enabled ="true" pageOutput ="false" requestLimit ="20" traceMode ="SortByTime " /> </system.web> </configuration> In Above example, pageOutput="false" and requestLimit="20" are used, so

Viewing Trace Data

Tracing can be viewed for multiple page requests at the application level by requesting a special page called trace.axd

<system.web> <compilation debug="false" /> <authentication mode="Windows" /> <trace enabled ="true" pageOutput ="true" /> </system.web>
protected void Page_Load(object sender, EventArgs e) { System.Diagnostics.Trace.Write("This is Page_Load method!");

You might also like