October 21, 2016
Hot Topics:

Tapping Into the Logging Application Block

  • January 23, 2006
  • By Vijay Mehta
  • Send Email »
  • More Articles »

Extending the Logging Application Block

EL2 and the Logging Application Block are highly extensible frameworks that have many predefined extension points. In this next example, you'll get a closer look at one of the extension points, the Custom Trace Listener, by creating a rolling flat file trace listener.

Note: The purpose of this next example is to demonstrate the extensibility of the logging application block. The example has not been tested for production use and probably is not the most efficient method for creating a rolling log file.

At this point, it is important that you download the source code so you can follow along with the text. I am not going to present all of the details on the System.Diagnostic.TraceListener class because there is already a ton of information out there on MSDN. I will outline the necessary steps required to create a custom trace listener in the context of the Logging Application Block.

First, to wire up a custom trace listener and to enable it for use with the Console, you must do two things: inherit from Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.CustomTraceListener class and decorate your new class with the following attribute:


Now, understand that you can manually go through and hack your application configuration files, so technically speaking the attribute is not absolutely necessary, but it is a significant time saver.

After you create a new class that meets those two requirements, it is as easy as opening the console and configuring your "Custom Trace Listener." This is accomplished the same way you configured the Database Trace Listener with two exceptions. First, you will have to specify the "type" attribute (right pane of the console), which is the location of the assembly with the Custom Trace Listener. Second, for this example you will need to configure the attribute collection, which is a group of name value parameters for use at runtime:

Figure 2: Use the EL2 console to configure name-value pairs for use by the Custom Trace Listener.

In this example, you are creating a new attribute called "fileName" and giving it a value of "CustomLog{Date}.txt". The "{Date}" string is used by the custom TraceListener as a token to enable the string as a replacement value. The attribute system is actually not new with EL2; rather, .Attribute is a StringDictionary property that hangs on the System.Diagnostic.TraceListener base class.

What's new with EL2 is the plumbing code that automatically interprets the information stored in the app.config file. Take a look at the following excerpt from the app.config file. As you can see, the attribute "fileName" has been added to the listener configuration section automatically, and is interpreted natively by EL2.

   <add fileName="CustomLog{Date}.log" listenerDataType="..."
   name="Custom TraceListener" initializeData="" />

The last thing you need to do before writing some code to use this new trace listener is create a new category called "Fatal." Enable the new "Fatal" category with the new Custom Trace Listener. Once you have saved, add some code to test out the new Custom Trace Listener. Rather than logging to a single category (as in the previous example), use one of the new features in the Logging Block by utilizing the two categories created so far, "Audit" and "Fatal":

LogEntry le = new LogEntry();
le.Message = "Testing both our Custom Trace Listener and our
              Database Trace Listener";
le.EventId = 1234;
le.Title = "Two Categories";
le.Priority = 1;

Logger.Write() has 19 overloads, so nearly every way you would want to log a message (in other words, Generics, string parameters, and so forth) is available.

The Easier, the Better

This article has only scratched the surface of the new Logging Application block, but I hope that it has gotten some of you thinking about how EL2 can improve your code. The Enterprise Library is a free, reusable, battle-tested set of frameworks that can greatly reduce your time to market or improve your existing code base.

Related Links


Source code: EL2LoggingProject.zip

About the Author

Vijay P. Mehta works in enterprise architecture for a Fortune 500 company in Indiana, where he uses VS.Net to design, develop, and architect enterprise solutions. Reach him at vijay@mehtasolutions.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel