EntLib 5 - Looking for advice on ConfigureDAAB()

Topics: Building and extending application blocks, Data Access Application Block, Logging Application Block
Aug 9, 2011 at 2:47 PM

Hi all -

I am attempting to run a ConfigureDAAB() method but am encountering errors (see a previous post of mine issue 268328). I decided to place a call to the method in my WPF application entry point (App()).

Code

public App()
        {
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);

            ConfigureDAAB();
            
        }

        public void ConfigureDAAB()
        {
            //http://dotnet.dzone.com/news/enterprise-library-5-fluent
            
            ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
            
            // hard-coded for first attempt
            builder.ConfigureData()
                    .ForDatabaseNamed("MPA_LOG")
                    .ThatIs.ASqlDatabase()
                    .WithConnectionString("Database=xxxx;Server=xxx;User id=xxx;Password=xxx;")
                    .AsDefault();

            var configSource = new DictionaryConfigurationSource();
            
            builder.UpdateConfigurationWithReplace(configSource);

            EnterpriseLibraryContainer.Current
              = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

        }

What I really want to do is simply override the LOG database specified in my app.config.  Am I getting close to the correct solution?

 

thanks a lot,
Brent

 

Aug 10, 2011 at 3:13 AM

Hi Brent,

Are you still getting the error from the previous thread? Moving on, if I understand correctly your question, you want to change your database settings at runtime through Fluent Config API? Is this correct? I'm afraid the call to UpdateConfigurationWithReplace will create a new configuration settings. To avoid this, you can try the approach found on this thread. If you have further questions, just let me know :)

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 10, 2011 at 4:12 AM

Hi Noel -

The suggested thread sounds like a useful feature add, but I don't believe this is what I'm looking for.  I want to replace the coded databaseInstanceName below from xxx_LOG to yyy_LOG, depending on select client settings; for instance, if my client is a California-based user they would write to the California_LOG, and if they are London-based they would write out to London_LOG.  I proved I could do this by editing the app.config before attempting to load the app, but it seems instable and I thought EntLib5 solutions would be more elegant.  I just don't know, though ...  Is this possible?

<add name="Database Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
        databaseInstanceName="xxx_LOG" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
        traceOutputOptions="None" filter="All" />
thanks,

Brent
Aug 10, 2011 at 5:23 AM

I believe this is possible. The link I gave you, though not exactly the code you are looking for, gives the idea. I'll try it first on my side and get back to you for updates.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 10, 2011 at 6:00 AM
AvanadeSupport wrote:

I believe this is possible. The link I gave you, though not exactly the code you are looking for, gives the idea. I'll try it first on my side and get back to you for updates.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us


After reviewing your post in greater detail I must admit you're right -- it looks like I can apply a similar solution for logging.  If I can get it to work in the next day, I will be sure to post what I came up with. 

I do have a relevant question for you -- would you ever consider changing the physical connection string of a <ConnectionString> you have already set up in the app.config?  I would think if I changed this, then I wouldn't even have to worry about EntLib5 databaseInstanceName ... it would just look for the alias I already have in my app.config (example).

<add name="xxx_LOG" connectionString="Database=xxx_LOG_DEV;Server=server32,3180;uid=xxx;pwd=xxx;"
      providerName="System.Data.SqlClient" />

change the Database from xxx to yyy

<add name="xxx_LOG" connectionString="Database=yyy_LOG_DEV;Server=server32,3180;uid=xxx;pwd=xxx;"
      providerName="System.Data.SqlClient" />
Aug 10, 2011 at 6:18 AM

I got it worked and here is my code snippet:

            //Get the settings from configuration file
            var source = new FileConfigurationSource(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            ConfigurationSourceBuilder configBuilder = new ConfigurationSourceBuilder();

            //Get the existing logging config section                 
            LoggingSettings logginConfigurationSection = (LoggingSettings)source.GetSection("loggingConfiguration");
            
            //Get the specific database trace listener
            FormattedDatabaseTraceListenerData databaseTraceListener = (FormattedDatabaseTraceListenerData)logginConfigurationSection.TraceListeners.Get("Database Trace Listener");
            
            //Change the instance name.
            databaseTraceListener.DatabaseInstanceName = "AnotherLog";

            //Add the section to the configuration builder
            configBuilder.AddSection("loggingConfiguration", logginConfigurationSection);


            var configSource = new DictionaryConfigurationSource();
            configBuilder.UpdateConfigurationWithReplace(configSource);

            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

            LogWriter myWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            myWriter.Write("Log Success!");

 Regarding with your second question, yes you can do that. It really depends on your requirements.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 10, 2011 at 6:34 AM

Which DLLs/namespaces provide me dotting into LoggingSettings and FormattedDatabaseTraceListenerData?

Aug 10, 2011 at 6:59 AM

DLLs

Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll

Namespaces

using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration;

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 18, 2011 at 9:35 PM

Hi Noel -

Success!  We had a week of preparing for a demo and I had to de-prioritize this deliverable.  Had time to get back on it today and *poof* it worked!

 

Thanks again,
Brent