How do I modify Connection String for Logging

Topics: Exception Handling Application Block, Logging Application Block
Dec 31, 2009 at 5:54 PM
Edited Dec 31, 2009 at 6:36 PM

I need to programatically modify the connection string that the Exception Handling Application Block uses to connect to the logging database.  I only have an single point of access to the Exception Handler. I would hope that I can use the Configuration Context to modify the connectionstrings in memory and pass this modified context to the process. Like this:

 public class BaseException : Exception
    {
        public BaseException(string message, Exception ex, string policy)
            : base(message, ex)
        {
            ConfigurationContext ctx =  new ConfigurationContext();
            // do something here
            ExceptionPolicy.HandleException(ex, policy, ctx);
        }
    }
I would like to be able to get the current configuration context and modify that and pass that to the exception handler.
Jan 4, 2010 at 9:42 AM

Hi,

What version of ent lib are you using, i cant seem to find the ConfigurationContext class. Please see this "Updating Configuration Settings at Run-time" http://msdn.microsoft.com/en-us/library/cc511916.aspx . May I know the reason for wanting to change the connection string at run time? Do you have multiple logging database wherein you would need to change the connection string for each exception handling policy?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 4, 2010 at 3:18 PM
Edited Jan 7, 2010 at 3:41 PM

I am using ver. 1.1. The purpose is that we are not allowed to have passwords in clear text so we put the encrypted string in the configuration files. Then at runtime we decrypt the string. This works fine when I create a database using  DatabaseFactory I just make a call that returns the DB via a custom security dll. However I do not fully understand the best way to pass the context to the Logging dll after decryption of the password.

 

Jan 5, 2010 at 5:00 AM

Hi,

AFAIK, there is an option in the enterprise library to encrypt the connection strings in the config. Please see this: http://msdn.microsoft.com/en-us/magazine/cc163766.aspx in the Security topic.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 7, 2010 at 3:53 PM

I have the solution here:

ConfigurationContext ctx = ConfigurationManager.GetCurrentContext();
    DatabaseSettings sec = (DatabaseSettings)ctx.GetConfiguration("dataConfiguration");
    foreach(ConnectionStringData con in sec.ConnectionStrings)
    {
     if(con.Name =="%connectionStringName%")
     {
      ParameterDataCollection pc = con.Parameters; 
      if (pc != null)
      {
              
      //do what is needed to to decrypt connectionstring 
      //using the 'pc' parameter collection
      // Then call the HandleException passing the new context
       ExceptionPolicy.HandleException(ex, policy, ctx);
      }
     }
    }