Debugging

I l @ ve RuBoard

Rather than printing comments to your pages or using other jury-rigged debugging, you can use ASP.NET's built-in debugging features.

System.Trace

The System.Trace class allows you to output both information and warnings, depending on the situation, using Trace.write() and Trace.warn() . You can combine these with exception handling to provide extensive debugging information.

 <%@ Page Language="VB" Trace="True"  %> <script runat="server" language="VB"> Function Savings (price as Double, discount as Double) as Double     if TraceContext.isEnabled then        Response.write "<!-- Tracing enabled.  Sort mode is " & _                                            TraceConect.TraceMode&"-->"     else        Response.write "<!-- Tracing disabled. -->"     end if     dim temp as Double     try        Trace.write("SavingsCategory",                   "Attempting calculation: price="&price&" discount="&discount)        temp = discount/price     catch E as Exception        Trace.warn("SavingsCategory", "Invalid savings or price:", E)        temp = 0     finally        return temp     end try end Function </script> <html> <head><title>Debugging Example</title></head> <body>     This week's discount is <%= Savings(299.99, 50.00) %>. </body> </html> 

The Trace methods both allow you to choose whether to include exception information:

 Public Warn (category As String, message As String) Public Warn (category As String, message As String, errorInfo as Exception) Public Write (category As String, message As String) Public Write (category As String, message As String, errorInfo as Exception) 

Application-Level Tracing

The global.asax file gives you an opportunity to add generic error handling, which can include event log information:

 <script runat="server" language="C#"> public void Application_Error(Object sender, EventArgs e){     string LogName = "ASPAlliance";     string ErrorMessage = "URL: " + Request.Path + " Error: "                                   + this.LastError.ToString();     // Create event log if it doesn't exist     if (!System.Diagnostics.EventLog.SourceExists(LogName))     {         System.Diagnostics.EventLog.CreateEventSource(LogName, LogName);     }     System.Diagnostics.EventLog Log = new System.Diagnostics.EventLog();     Log.Source = LogName;     Log.WriteEntry(ErrorMessage,System.Diagnostics.EventLogEntryType.Error); } </script> 

Configuration

You can turn on tracing for the application through the web.config file. This file may contain a number of different sections, all specified via XML, but the relevant sections for turning on tracing are shown in the sample web.config file that follows . This example sets tracing to be enabled (the default is False ), to allow up to 20 simultaneous tracing requests (the default is 10), to output tracing information at the bottom of each page (the default is for it to be available only in the trace utility), for tracing information to be listed chronologically (it can also be listed by category), and for the trace utility to be available only locally, on the server.

 <configuration>     <configSections>         <sectionGroup name="system.web">             <section name="trace"                      type="System.Web.Configuration.TraceConfigHandler">         </sectionGroup>     </configSections>     <system.web>         <trace enabled="true" requestLimit="20" pageOutput="true"                traceMode="SortByTime" localOnly="true" />     </system.web> </configuration> 
I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net