Enterprise Library - Building a Custom Trace Listener
Enterprise Library - Building a Custom Trace Listener
Introduction
Early this week, I got a phone call from a customer.
The customer wanted to know how to build a new provider for logging application block in order to
log data to his log table and not to the suggested logging application block database.
Also, he wanted to know why to use the ent-lib at all.
I decided to write this post in order to show how to build such a provider and to answer why
I think you should use ent-lib.
Building a Custom Trace Listener
Building a new provider to the logging application block has some simple guidelines:
- Add using for the following namespaces:
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
- Derive your class from CustomTraceListener class.
- Add the following attribute:
[ConfigurationElementType(typeof(CustomTraceListenerData))]
- Override TraceData method which is called by the TraceData of the LogSource class.
Also override the Write and WriteLine methods which write the specified message
to the listener.
That is all there is in the implementation.
Custom Trace Listener Example
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class CustomTraceListenerExample : CustomTraceListener
{
#region Ctor
public CustomTraceListenerExample()
{
}
#endregion
#region
public override void Write(string message)
{
// implement the write action
}
public override void WriteLine(string message)
{
// implement the write line action
}
public override void TraceData(TraceEventCache eventCache, string source,
TraceEventType eventType, int id, object data)
{
if (data is LogEntry && Formatter != null)
{
// implement the action for the log entry data
}
else
{
// implement the action of the object you got probably you
// would use the Write method and write to the listener the object
// you got as string
}
}
#endregion
}
After you build the custom trace listener in order to use it in you application you should
do the following things:
Open the config file in the ent-lib designer.
Add a new custom trace listener.
Press the button of Type in the properties window.
Load the assembly that your custom trace listener is in it.
From now on you can use the new trace listener as you would have used other trace
listeners in ent-lib.
Why to Use Enterprise Library?
I gave a few lectures about ent-lib in the past and the question why to use ent-lib always raise.
To be honest I'm very subjective about this subject - I really love ent-lib.
But the following things I'm going to write are objective:
- The application blocks are proved technology with a large community behind them.
- The blocks were tested and are being tested by the patterns & practices team
and the bug rate declines.
- The blocks are integrate with one another and therefore have an advantage against
frameworks which were built to do only one thing like Log4Net for example.
- The blocks points to key features of application infrastructure - helps to develop
infra fast.
- Because the blocks are consistent, once you learn how to use a block it's
easy to learn how to use other blocks.
- Ent-lib has a designer tool for configurations that is integrated in visual studio.
The designer helps to configure the blocks and decrease the need to write
config files by ourselves.
- The application blocks simplify the code you write by providing a
convenient and readable code.
- The blocks are extensible - build your own providers or own block
(with the application block software factory).
There are more reasons to use ent-lib but these are the important ones.
Hope I helped you to decide to use ent-lib.