Trace tracking for ASP.

Source: Internet
Author: User
Tags httpcontext

The debugger is the developer's best helper when it comes to catching bugs.
However, ASP. NET tracking, which is a great non-negligible aid in debugging, allows your ASP to output messages during execution, providing useful information to help you find the problem.

The debugger and the trace are a little different from the two techniques.
The debugger is a natural interaction technology that relies on your ability to pause the program and check the current state.
Tracking is a kind of tracking information (similar to classic "printf" debugging) that does not interfere with the operation of the system and is set up by a simple output code, and at the end of execution you can get the output generated by the tracking code and analyze it.

Asp. NET tracking subsystem through a control, a page or a component that can increase its own trace output exposes an API that can output any information available to you for meaningful help.
In ASP., the trace is tightly integrated with the page and the runtime.

can be done by

<%@ page trace= "true"

Enable tracing on a separate page, in which case the trace information is sent to the browser as part of the page to display.

You can also modify the Web. config

<trace enabled= "true" >

Enable tracking of the entire station, via http://Site/trace.axd can be tracked access.

Tracking is an infrastructure built as a subsystem to ASP. Provides diagnostic information about a single request to the application.
Through tracking, you can analyze the data exchanged between the browser and the Web server, including request details, time leveling information, server variables, Cookies, header information, session status, and so on.

Code Add trace mechanism

In the page or its inheriting class
Trace.Write ("Your message");

anywhere in the ASP. HttpContext's Trace property exposes an instance of the TraceContext class to the developer
HttpContext.Current.Trace.Write ("Your message");

The TraceContext class also has a Warn method, which, unlike write, renders the text red. Both write and warn have some overloaded methods:

Write Overloads Reload
public void Write (String message)
public void Write (string category, String message)
public void Write (string category, String message, Exception errorinfo)

Match Warn Overloads
public void Warn (String message)
public void Warn (string category, String message)
public void Warn (string category, String message, Exception errorinfo)

The first overload writes a trace message to the trace log.
The second overload accepts the tracking category and trace content, the tracking category can use short tracking information, or it can be any string that is useful to the application or message.
The third overload writes trace information to the trace log, including all user-defined categories, trace messages, and error messages. The trace log does not accept HTML formatting and the text is escaped before rendering to the browser.

For example:

Trace.Warn ("Tracking Classification", "Tracking information", New Exception ("Error, data object is empty");

Shown as:

Other two properties of the TraceContext class:
The IsEnabled property is a Boolean type that indicates whether tracing is enabled for the current WEB request.
The TraceMode property sets the sort order of the trace message output, an enumeration type that contains SortByCategory (classification) and SortByTime (time).

You can write program settings isenable to enable tracing.

in ASP. NET 2.0, you can also use the traceenabled of the new property page class, which is just the wrapper for trace.isenabled.

public class Page:templatecontrol, IHttpHandler
{
Sets a value that indicates whether tracing is enabled for the System.Web.UI.Page object.
True if tracing is enabled for the page, otherwise false. The default value is False.
[Browsable (False)]
[Editorbrowsable (Editorbrowsablestate.never)]
[DesignerSerializationVisibility (Designerserializationvisibility.hidden)]
public bool TraceEnabled {get; set;}
Sets the display mode of the trace statement in the page.
System.Web.TraceMode One of the enumeration members.
[DesignerSerializationVisibility (Designerserializationvisibility.hidden)]
[Browsable (False)]
[Editorbrowsable (Editorbrowsablestate.never)]
Public TraceMode Tracemodevalue {get; set;}

......

}

The Web. config configuration in ASP. 2.0 is as follows:

<trace
Enabled= "True|false"
Localonly= "True|false"
Mostrecent= "True|false"
pageoutput= "True|false"
requestlimit= "Integer"
Tracemode= "sortbytime| SortByCategory "
Writetodiagnosticstrace= "True|false"/>

Asp. NET built-in trace viewer--trace.axd,

Trace.axd is a system-defined HTTP handler that gets the cache of internal trace information and displays it to the Web interface.

Access the trace viewer by requesting the Trace.axd (HTTP//web site/trace.axd) under the application root directory.

You can also add the id=xxx parameter to the page request (http://Site/trace.axd?id=0 start) to directly access a specified trace information.

Attention:
1. Attribute enabled
The Trace.axd tool is activated by enabled= "true", but the trace information is not activated as part of the page output.
This enables tracing to output that does not affect the user's page.
Doing so allows you to open the trace in the product without affecting the output of the user's page. See the reason (2 attribute pageoutput)
Such as:
<trace enabled= "true"/>
2. Attribute pageoutput Indicates whether trace output is displayed in any page
The default is False, so the configuration
<trace enabled= "true" pageoutput= "false"/>
You can omit pageoutput= "false".

All trace information is cached in a DataSet object by the TraceContext class,
When the HttpRuntime object for the AppDomain of an application is first initialized,
The TraceContext class is instantiated.
The result is that when Trace.axd is enabled, the more trace information is maintained, the more memory is consumed.

A new feature of ASP. NET 2.0 is that it allows the use of tracecontext.tracefinished events.
Thrown after all request information has been collected.
The tracefinished handler provides a collection of Tracefinishedeventargs parameter accept trace Message (Tracerecord) objects.
Each store has a separate ASP. NET trace message and all related data,
Allows you to collect this information and handle it at will,
Display to a page or record to your own database, and so on.

Asp. NET is directly related to the. NET Framework, and the Systems.Diagnostics namespace defines two classes: Trace and Debug.
They provide a common way to track code.
Trace and debug classes are essentially the same, working on a number of specific modules, such as listeners.

The listener collects and stores messages to the Windows event log, text files, or other similar files.
Each application can have its own set of listeners;
All registered listeners accept all of the published messages.
The tracking subsystem in ASP. NET 2.0 is further enhanced to forward the ASP.
Enable this feature by opening the writeToDiagnosticsTrace property of the Web. config file <trace> section
As shown below:
<trace enabled= "true" pageoutput= "false" writetodiagnosticstrace= "true"/>
However, just setting the writeToDiagnosticsTrace property does not work, and you need to register one or more listeners with the program.
Can be achieved through the <trace> section in the <system.diagnostics> section,
For example:
<system.diagnostics>
<trace autoflush= "true" >
<listeners>
<add name= "MyListener"
Type= "System.Diagnostics.TextWriterTraceListener"
Initializedata= "C:\myListener.log"/>
</listeners>
</trace>
</system.diagnostics>
Here are two things to note:
First, only text can be passed to the listener via write or warn, that is, the listener does not store a standard ASP. NET trace information, but only the contents of the tracking information block.
Second, you can write a custom listener to send trace information to the media of your choice-such as a SQL Server database-all you have to do is inherit System.Diagnostics.TraceListener and reload some methods.
And then
Register the new class in the Web. config file for the website you want to track.
(For more information on writing custom TraceListener, please refer to msdn.microsoft.com/msdnmag/issues/06/04/clrinsideout.)

One interesting thing to note is that the System.Web.dll in ASP. NET 2.0 adds a new type WebPageTraceListener from TraceListener, which is used with writeToDiagnosticsTrace.
When the WebPageTraceListener instance is added to the trace listener collection, trace messages written to the. NET trace log will also be written to the ASP trace log.

Finally, a new <deployment> section is added to the Web configuration section in the trace related in ASP. NET 2.0.
<deployment retail= "True|false"/>
When Retail is true, ASP. NET disables certain settings that are defined in the configuration, such as trace output, custom errors, and debugging capabilities.
The <deployment> section can only be set in Machine.config.
Translator: This section applies only to machine-level and application-level configuration files. 】

Trace tracking for ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.