Enterprise Library 2.0 Logging Application Block Part I - TraceListeners
by David Hayden ( .NET Developer )
The Enterprise Library 2.0 Logging Application Block makes logging various application events in your winform and web applications a snap. Before we dig into implementation specifics of the Logging Application Block, we first need to understand the importance and role of TraceListeners.
TraceListeners
The heart of the Enterprise Library 2.0 Logging Application Block is its TraceListeners, which are essentially the conduit for which event messages get to their intended destinations. Enterprise Library 2.0 provides the following TraceListeners:
- Database TraceListener
- Email TraceListener
- Flat File TraceListener
- Formatter Event Log TraceListener
- Msmq TraceListener
- System Diagnostics TraceListener
- WMI Trace Listener
The usefulness of each TraceListener is fairly obvious. You would use the Database TraceListener to log trace messages to a database. The Email TraceListener is for sending trace messages via email. You get the idea.
All of the Enterprise Library 2.0 TraceListeners derive from the base TraceListener Class that is a part of the .NET 2.0 Framework.
TraceListener Basics
The TraceListeners in the Logging Application Block work similar to the TraceListeners in the .NET 2.0 Framework. If we use the Flat File TraceListener as an example, its use is not much different than the TraceListener Class it derives from - the TextWriterTraceListener Class in System.Diagnostics. One can use the TextWriterTraceListener Class to output trace information to a text file with some really simple code:
// Create a log file
Stream logFile = File.Create("TraceOutput.log");
// Create the TextWriterTraceListener
TextWriterTraceListener listener =
new TextWriterTraceListener(logFile);
// Add the listener to the list of TraceListeners
Trace.Listeners.Add(listener);
// Output the message
Trace.Write("My log message");
// Flush the output.
Trace.Flush();
The code above programmatically adds a new TextWriterTraceListener to the Trace Class' Listener Collection which means that any messages sent via Trace.Write or Trace.WriteLine will now also be logged to our log file, TraceOutput.log.
However, nobody is going to write such code, because your app.config or web.config class allows you to define TraceListeners in it without touching a bit of code. This allows you to add and remove TraceListeners used by your application by manipulating your configuration file without changing a single line of code:
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="LogFileListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="TraceOutput.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
In the configuration snippet above, we have essentially defined a new TraceListener, LogFileListener, that outputs trace messages to our same log file, TraceOutput.log. The code in our application has now been abstracted from where the trace information is being saved and has been reduced to:
// Output the message
Trace.Write("My log message");
// Flush the output.
Trace.Flush();
Benefits of Logging Application Block
In Part 2, we will start to see how the Enterprise Library 2.0 Logging Application Block extends this foundation provided in the .NET 2.0 Framework.
Not only does Enterprise Library 2.0 introduce all the custom TraceListeners mentioned above that are not included in .NET 2.0, but the Logging Application Block also introduces filters, message categories, message priorities, and ways to push messages based on category only to specific TraceListeners.
All of this gives us additional flexibility and manageability when it comes to logging trace messages using a very simple facade class that doesn't introduce any additional complexity.
Source: David Hayden ( .NET Developer )
Enterprise Library 2.0 Tutorials