New
attribute “mostRecent” is introduced in Trace configuration settings in
.config files for specifying which trace messages to keep in
the trace store if the requestLimit value exceeds. In ASP.NET 1.x, if the
requestLimit exceeds, it will stop collecting trace messages in trace store
unless until you clear trace store or restart that application appdomain. Now
the Trace settings in config file will looks like,
<trace
enabled="false"
localOnly="true"
mostRecent=”true”
pageOutput="false"
requestLimit="10"
traceMode="SortByTime"
/>
When
you set this attribute value to true and if the requestLimit values exceeds,
Trace store will keep the most recent trace messages in the store and old
message are discarded. If you set this attribute value to false, trace store
will stop collecting trace message once its requestLimit value exceeds.
Programmatic Access to Trace Messages
ASP.NET
allows programmatic access to trace messages for each request in tracefinished
event. TraceFinished event is raised by tracecontext after it gathers all the
trace information about a request. In this event you can access all the trace
messages for that request. You can use this feature to redirect certain request
trace messages to different output source and for changing the display format
of the trace message for certain request. For example, if you want to display
trace message in the page itself(like pageoutput) for one page alone, so that
other users wont see trace message in their pages. You need to write the
following code in that aspx page,
Private
Sub Page_Load1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
AddHandler Trace.TraceFinished, AddressOf
OnTraceFinished
End
Sub
Private
Sub OnTraceFinished(ByVal sender As Object, ByVal e As TraceContextEventArgs)
Dim
r As TraceContextRecord
For
Each r In e.TraceRecords
Response.Write(String.Format("Category:{0}
Trace Message {1} <BR>", r.Category, r.Message))
Next
End
Sub
In
the page_load or Page_init add the handler to tracefinished event and in the
tracefinished event handler write code for displaying your trace message in
that page.
Integration between system.diagnostics.trace and httpcontext.trace
There
are two types of tracing available in .NET Framework. One is
httpcontext.trace class which is used to write trace messages in
asp.net. Another system.diagonstics.trace
class is used in other components to write trace messages. Httpcontext.trace is
controlled by trace settings in config file, but
diagnostics.trace class is controlled by debug compilation code while
compiling any dll in .NET.
In
ASP.NET 1.x, trace message written using httpcontext.trace
and diagnostics.trace are collected
separately. These messages can not be merged into one source. But in ASP.NET
2.0, these trace messages can be merged.
You can display trace message written using diagonistics.trace
in trace viewer(trace.axd) along with trace message written using
httpcontext.trace. Similarly you can consume trace messages written
using httpcontext.trace in trace
listener written for consuming diagnostics.trace
messages. Because of this feature, you can easily track business flow from
asp.net pages to business components as all the trace messages are collected
together.
For
forwarding httpcontext.trace class trace messages to diagnostics.trace
class listeners, you need to set new attribute “writeToDiagnosticsTrace” in the
trace section in .config file like this,
<trace
enabled="true" pageOutput="true" localOnly="true"
writeToDiagnosticsTrace ="true"/>
If
you set “writetoDiagnosticsTrace” to true, all the trace message written in
asp.net using httpcontext.trace class will be consumed by trace
listeners for diagnostics.trace.
For
displaying system.diagnositics.trace messages in asp.net trace viewer,
you need to add WebPageTraceListener to listeners section of config file like
this,
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add
name="webListener"
type="System.Web.WebPageTraceListener, System.Web ,
Version=2.0.3500.0,Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</listeners>
</trace>
</system.diagnostics>
Trace message of system.diagnosticis.trace wont be collected if you
just enable trace in config file. You also need to define trace switch when you
compile your web application or business component. By default trace switch is
defined for business component, but for web application it wont be defined. So
you need to define it before you start collect diagnostics.trace messages
in asp.net
Following
are the two ways to define trace switch
-
Project Property Page
à
Build
à
Condition Compilation Symbols -> Mention trace in the text box
-
Specfying compilation option in config file like this for each language,
<system.codedom>
<compilers>
<compiler language="VB"
extension=".vb"
compilerOptions="/d:Trace=true"
type="Microsoft.VisualBasic.VBCodeProvider, System,
Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</compilers>
</system.codedom>
Conclusion
It
is always recommended to have trace messages in the code, this messages will be
very helpful to diagnose problems in production enviornment without editing the
code by just changing few setting in config file. Using this enhanced tracing
features in ASP.NET 2.0, you can easily track the business work flow by
integrating trace messages from asp.net to business components.