Entlib 5.0 Custom TraceListener - Runtime Values From App

Topics: Exception Handling Application Block, Logging Application Block
Sep 30, 2010 at 8:42 PM

Hello,

I have a custom TraceListener that inherits from FormattedTraceListenerBase and I would like to get access to values that are only available to me at runtime. (username and password)  The listener uses reflection to create an instance of a web service client proxy and execute a method to send log information back to the server and into the database.  If I have no security on the web service it works like a champ.  I even have it fully integrated with the configuration tool.  Very nice.

In my override of the Write method I'd like to get the username and password.  I am using Prism and can put things in the container but I can't find a way to get Entlib to use that container so I can retrive things at runtime.

I have tried this in the ConfigureContainer override in my bootstrapper:

GlobalSettings settings = new GlobalSettings();
settings.LogonInfo.UserName = "blah";
settings.LogonInfo.Password = "blahblah";

Container.RegisterInstance<GlobalSettings>(settings);
Container.AddNewExtension<EnterpriseLibraryCoreExtension>();
base.ConfigureContainer();
IServiceLocator locator = new UnityServiceLocator(Container);

EnterpriseLibraryContainer.Current = locator;

This appeared to work but now I've figured out that even if I don't register an instance in the bootstrapper, I still get one in the Write method with:

EnterpriseLibraryContainer.Current.GetInstance<GlobalSettings>();

Is there a way to get the EnterpriseLibrary to use the container that is already in my application?  Is this perhaps the wrong approach to pass runtime information to the listener?

I am already creating a GenericPrincipal for use in my application.  I could extend that or the GenericIdentity and then just get the information from:

Thread.CurrentPrincipal.Identity.Name;

I know that that will work but it made me feel dirty.  :)

Thanks for any pointers you can offer.

 

Sep 30, 2010 at 9:10 PM

[wipes-egg-from-face]

It's only minutes after my original post and I've solved the problem.  The key is to provide a name for the instance you are registering in the bootstrapper:

Container.RegisterInstance<GlobalSettings>("instanceName", settings);

And then when you want to retrieve it in your TraceListener:

EnterpriseLibraryContainer.Current.GetInstance("instanceName");

Works like a champ.

I was sure that I tried that but it's been a long day so...

Dave Totzke