Search code examples
azureenterprise-librarytracelistenertrace-listener

How to dynamically/programmatically set the Enterprise Library Database Trace Listener connect string


Is there a way to dynamically/programmatically set the connection string the EntLib 5 database trace listener utilizes?

I am hosting my WCF service in Windows Azure, which means I will not have access to the web.config once it is deployed. In order to eliminate the need to redeploy my solution whenever I want to point the DB trace listener at a different DB I was hoping there would be a way I could pull that setting from my service configuration file and set it dynamically.


Solution

  • Yes, you can use the fluent configuration as described in this post:

    var builder = new ConfigurationSourceBuilder();
    builder.ConfigureData()
            .ForDatabaseNamed("MyDb")
            .ThatIs.ASqlDatabase()
            .WithConnectionString(RoleEnvironment.GetConfigurationSettingValue("MyConnectionString"))
            .AsDefault();
    
    builder.ConfigureLogging()
            .WithOptions
            .LogToCategoryNamed("General")
            .SendTo
            .Database("Formatted Database TraceListener").UseDatabase("MyDb")
            ...;
    
    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);