Ent Lib 6 / Unity 3: Injecting a dependency into a CustomTraceListener

Oct 8, 2013 at 6:16 PM

I am using Enterprise Library 6 and Unity 3 throughout my solution to manage cross cutting concerns and as a lightweight container for managing Dependency Injection. I have a requirement to develop a Custom Trace Listener. I need to inject a dependency into that Custom Trace Listener.

This functionality is available in Enterprise Library 5 and Unity 2. The following link provides a nice example of how it can be achieved (my own requirements are almost similar to those outlined in the link):


However, it seems as though it is not a straightforward migration in Enterprise Library 6, for the following reasons:

The Ent Lib 6 TraceListenerData class no longer makes available an override against the:


...method. In Ent Lib 6 the new functionality is provided in the:


...method. My understanding is that the Ent Lib 5 GetCreationExpression() allowed 'marker's' to be placed in the returned expression that could be substituted when the Unity Container was resolved. The new CoreBuildTraceListener() returns a new instance of the Trace Listener.

Also, Ent Lib 6 no longer uses the Unity container to resolve instances. The:


...has been removed. The new approach would be something along the lines of:


However, for me this means that the CoreBuildTraceListener() is invoked (on logWriterFactory.Create()) before any call can be made to resolve a Interface through the Unity Container.


Using Enterprise Library 6 and Unity 3, is it possible to either:

a. Get a hold of the Unity Container in the CoreBuildTraceListener() method at the correct time - so I can find the appropriate Interface I need to inject into my Custom Trace Listener when the Custom Trace Listener is created or...

b. Is there a new approach to achieving the same end-goal that I don't know about that you could advise me on (if possible, please could you provide a simple example?) or...

c. Has this functionality regressed?

Thanks in advance.
Oct 9, 2013 at 6:35 AM
One of the goals of Enterprise Library 6 was to simplify; one of the simplifications was the removal of a dependency on Unity (or any other DI container). Without a container some techniques are no longer available (e.g. static marker class) and some tricks no longer apply (e.g. swapping an Enterprise Library implementation for a custom implementation in the container).

If you are developing a custom trace listener you would be able to use Unity inside the CustomTraceListenerData class (used by the configuration infrastructure). Enterprise Library will not make any container available (since it does not know about any containers) so you would have to access Unity via a singleton or static method/property. That is only if your trace listener supports configuration. The "typical" approach is to use "an extremely flexible programmatic approach to configure the blocks". So, if you follow that approach you could resolve the custom trace listener from the container and use that to configure the block programmatically.

Randy Levy
Enterprise Library support engineer
Support How-to
Marked as answer by scfox on 10/9/2013 at 11:27 AM