I have been searching for a while now without 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 Trace 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.
Found "a way" to complete what I was wanting to do. If anyone has any suggestions or improvements please reply.
//Find the configuration file in with the calling assembly and get the path.
//Do not feel this is ideal.
var assembly = Assembly.GetCallingAssembly();
var path = Path.Combine(Path.GetDirectoryName(assembly.Location),
@"ApplicationBlocks.config");
//Load the config file and make sure it gets cleaned up.
using(var fileSource = new FileConfigurationSource(path))
{
//Create the config builder and configure the sections you want to override.
var configBuilder = new ConfigurationSourceBuilder();
configBuilder.ConfigureData()
.ForDatabaseNamed("Data_Connection")
.ThatIs
.ASqlDatabase()
.WithConnectionString("ConnectionString Here")
.AsDefault();
//Update the file source configuration section with the ones in the builder.
configBuilder.UpdateConfigurationWithReplace(fileSource);
//Set the current container to the one we just created.
EnterpriseLibraryContainer.Current =
EnterpriseLibraryContainer.CreateDefaultContainer(fileSource);
//Get instance of log writer in this case.
_logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
}
Marked as answer by SaNtoG on Nov 02, 2010 04:17 PM
SaNtoG
Member
48 Points
17 Posts
How to load "Database Instance" for "Database Trace Listener" at runtime
Nov 01, 2010 07:19 PM|LINK
Hello,
I have been searching for a while now without 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 Trace 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 any code sample much appreciated.
Thanks,
Jeff
Logging Enterprise Library 5.0
SaNtoG
Member
48 Points
17 Posts
Re: How to load "Database Instance" for "Database Trace Listener" at runtime
Nov 01, 2010 10:35 PM|LINK
Found "a way" to complete what I was wanting to do. If anyone has any suggestions or improvements please reply.
//Find the configuration file in with the calling assembly and get the path. //Do not feel this is ideal. var assembly = Assembly.GetCallingAssembly(); var path = Path.Combine(Path.GetDirectoryName(assembly.Location), @"ApplicationBlocks.config"); //Load the config file and make sure it gets cleaned up. using(var fileSource = new FileConfigurationSource(path)) { //Create the config builder and configure the sections you want to override. var configBuilder = new ConfigurationSourceBuilder(); configBuilder.ConfigureData() .ForDatabaseNamed("Data_Connection") .ThatIs .ASqlDatabase() .WithConnectionString("ConnectionString Here") .AsDefault(); //Update the file source configuration section with the ones in the builder. configBuilder.UpdateConfigurationWithReplace(fileSource); //Set the current container to the one we just created. EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(fileSource); //Get instance of log writer in this case. _logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); }