Thursday, January 8, 2009

Enable Tracing in Shared components Using Listeners (Web and Non Web Application):

Enable Tracing in Shared components (Web and Non Web Application):

To enable tracing in business components which are used by both web and non web application,we need to implement trace without using the HTTPContext.(Components which are outside the application as well)

This can be done using Listeners which are inherited from Trace.Listener class.

A listener is an object that receives the trace output and outputs it somewhere; that somewhere could be a window in your development environment, a file on your hard drive, a Windows Event log, a SQL Server or Oracle database, or any other customized data store


.Net Framework uses the concept of Trace Listener in handling the Trace messages.In this context we need to create a Listener that inherited from trace.Listener class and add this to the Listener collection in the Web.Config File.
When we enable tracing in the application by default Single Listener is added to the Listeners Collection.If we want add more listeners we need to add to the listener collection as well Programatically in web.config file.


  <system.diagnostics>

<trace autoflush="true" indentsize="0">

<listeners>

<add name="ComponentListener"

type="Myproject.Examples.compListener, esamples" />

</listeners>

</trace>

</system.diagnostics>

The custom TraceListener in our example overrides the Write and WriteLine methods to write the passed message to the current HTTP context.

Both the Trace and Debug classes share the same TraceListenerCollection object; therefore, if you add a listener to Trace object, it will also be available to Debug object, and vice versa.

Some of the methods of Trace Listeners:
Fail : Outputs the specified text with the Call Stack.

Write :Outputs the specified text.
WriteLine :Outputs the specified text and a carriage return.
Flush :Flushes the output buffer to the target media.
Close :Closes the output stream in order to not receive the tracing/debugging output.


Once the Listeners are added to the web.config file.

In the component created.

Import System.diagnostics and use trace.write methods to write trace to output.


There ENDS Tracing in the components implementing Listeners

No comments:

Post a Comment