Creating Custom Trace Listeners

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

In some cases, the trace listeners provided with the Logging Application Block will not satisfy your application's requirements, and you will need to create your own trace listeners.

Typical Goals

In this scenario, the goal is to create a custom trace listener that sends the logging message to a destination other than the ones used by the trace listeners provided with the Logging Application Block.

Solution

Create a new class for the trace listener. Specify that it inherits from the CustomTraceListener class. Override the TraceData method to****format the LogEntry object and write the information to the output destination.

Configure the application to use the Logging Application Block. Use the Enterprise Library configuration tools to create a new custom trace listener and specify your trace listener's type name and any other custom configuration properties. Create or modify existing categories to use the trace listener as a destination.

QuickStart

For an extended example of how to create a custom trace listener that outputs logging messages to a Visual Studio output window, see Walkthrough: Creating a Custom Trace Listener.

Usage Notes

The Logging Application Block trace listeners derive from the TraceListener class in the System.Diagnostic namespace. This means that applications that do not use the application block can still use the application block trace listeners. To allow your custom trace listener to execute correctly when it is used outside the Logging Application Block, you should do the following:

  • In the TraceData method, test to see if the data parameter is of type LogEntry. If it is not of type LogEntry, use the ToString method to format the object.
  • Override the Write and WriteLine methods.