Specify DataBase Section for Database Logging at Runtime

Topics: Data Access Application Block, Logging Application Block
Nov 1, 2010 at 5:44 PM


I have been searching for a while now with no success. On how when using EntLib 5.0 Logging, Data; to be able to set the database (i.e. the connection string) at runtime for a database logging listener. The reason being we will have the same configurations on all levels of the environments (dev, test, stage, prod), but the connection string is different for each and we do not store the connection string data in the config file. The connection string data is loaded using a custom configuration manager for security reasons.


Any help and complete code sample much appreciated.




Nov 2, 2010 at 3:44 AM

Here's a sample code:

Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); 

LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName); 

FormattedDatabaseTraceListenerData databaseTraceListener = loggingSettings.TraceListeners.Get("Database Trace Listener") as FormattedDatabaseTraceListenerData ; 

databaseTraceListener.DatabaseInstanceName= newDatabaseInstance; 


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Nov 2, 2010 at 3:38 PM

Thank you for your response. I will look over the code and see how it will work. I came up with this alternative in the mean time. Please any feedback or other suggestions is greatly appreciated.

var assembly = Assembly.GetCallingAssembly();
var path = Path.Combine(Path.GetDirectoryName(assembly.Location),

using (var fileSource = new FileConfigurationSource(path))
     var configBuilder = new ConfigurationSourceBuilder();


     EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(fileSource);

     _logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

Nov 2, 2010 at 11:54 PM

Yes, that would also work and uses more intuitive code.  I admit I forgot that you could make use of an existing FileConfigurationSource so you don't have to rebuild the entire configuration when working with the Fluent Configuration API.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Nov 3, 2010 at 4:00 PM

Thanks a lot for your help!