Using the fluent api can I add a new tracelistener during runtime

Topics: Logging Application Block, Pre-release discussions
Jul 1, 2010 at 12:58 AM

While my application is running how can I add a new tracelistener via the fluent api, and then remove it later.

Jul 1, 2010 at 3:00 AM

You can do this using the fluent configuration api.  To add a new trace listener, say a flat file tracelistener:

 
var builder = new ConfigurationSourceBuilder();
var configSource = new DictionaryConfigurationSource();
builder.ConfigureLogging()
                .LogToCategoryNamed("File")
                    .SendTo.FlatFile("New Flat File TraceListener")
                       ////configure other options
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
              = EnterpriseLibraryContainer.CreateDefaultContainer(configurSource);

However, there's no direct API method which can remove a specific trace listener.  What I have in mind is if the desired effect is only to revert to the original configuration, you can do something like this:

EnterpriseLibraryContainer.Current
              = EnterpriseLibraryContainer.CreateDefaultContainer(ConfigurationSourceFactory.Create());

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

Jul 26, 2010 at 6:00 PM
Edited Jul 26, 2010 at 6:02 PM

If there is no way to remove a specific trace listener, then is there a way to remove all of them?

 

Or would I have to start with none, add them using the fluent api, and the revert to the original so I have none again?

 

My goal is to be able to reset a file a trace listener is using. I need the trace listener to close and let go of the file, then I can delete it, then the tracelistener can start writing to a new one. I thought if I could remove the trace listeners then I could accomplish this. Or perhaps there is another way?

Thanks!

 

 

Jul 27, 2010 at 3:16 AM
Edited Jul 27, 2010 at 3:18 AM

Hi,

As also suggested by Sarah the code below will reset your configuration. As far as I know it will be reset to the default configuration based on the application's configuration file which if in case you're not utilizing any application config file then it will be reset to none. 

EnterpriseLibraryContainer.Current
              = EnterpriseLibraryContainer.CreateDefaultContainer(ConfigurationSourceFactory.Create());

While if the only reason of removing a trace listener is to only remove the file locked, it is not actually the Trace Listener that keeps the file locked, it is the Logger/LogWriter instance. So if you want to delete the log file during runtime, one thing I can think of on how you can do this is to reset or disposed the Logger/LogWriter instance before you do the actual deletion of the file during runtime (Logger - Logger.Reset(); , LogWriter - writer.Dispose();). Then you can proceed with the next logging execution (if you're using a LogWriter object you need to instantiate its value again before proceeding into the next logging execution). Here's a related thread regarding this http://entlib.codeplex.com/Thread/View.aspx?ThreadId=216051.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 27, 2010 at 5:05 PM
Edited Jul 27, 2010 at 5:06 PM

Great, thanks! That did the trick. The logger.Reset() frees up the file. I don't think this is a bullet proof solution in a multi-threaded environment, so I may have to eventually implement starting with an empty config and configuring on the fly so I can revert back to no config.

 

Thanks again,

Pete