how to use encrypted connection string for logging

Topics: Data Access Application Block, Logging Application Block
Apr 27, 2011 at 2:42 PM

HI,

in my app all my connection strings are in encrypted by using some custom encrption algo. if i want use these connection string for logging messages to database , how i can pass these connection string?

Apr 28, 2011 at 3:04 AM

Hi,

One possible solution is you need to create a custom database provider that extends your database (lets say SQL Database) and pass the decrypted connectionstring on the base constructor. Here is a sample code:

 

ExtendedSqlDatabaseData.cs

namespace CustomDatabaseProvider
{
    public class ExtendedSqlDatabaseData : DatabaseData
    {
        public ExtendedSqlDatabaseData
            (ConnectionStringSettings connectionStringSettings, IConfigurationSource configurationSource)
            : base(connectionStringSettings, configurationSource)
        {
        }

        public override IEnumerable<Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration> GetRegistrations()
        {
            yield return new TypeRegistration<Database>(
               () => new ExtendedSqlDatabase(
                   ConnectionString))
            {
                Name = Name,
                Lifetime = TypeRegistrationLifetime.Transient
            };
        }
    }

 

ExtendedSqlDatabase.cs

namespace CustomDatabaseProvider
{
    [ConfigurationElementType(typeof(ExtendedSqlDatabaseData))]
    public class ExtendedSqlDatabase : SqlDatabase
    {
        public ExtendedSqlDatabase(string connectionString)
            : base(MyDecryptionAlgorithm(connectionString))
        {

        }

        private static string MyDecryptionAlgorithm(string encryptedConnString)
        {
            string decryptedConnString;
          
            //decryptedConnString = Your Decryption Algo Here

            return decryptedConnString;
        }
    }



 

This guide shows how to create a custom provider. Hope this helps.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 28, 2011 at 6:24 AM

hi Noel,

I created these two .cs files in my app. In ExtendedSqlDatabaseData.cs file, for GetRegistrations() i'm  getting the below error

 

'EncryptString.ExtendedSqlDatabaseData.GetRegistrations()': no suitable method found to override'

Do i need to add any methods in my aspx page or reference any dll to avoid this error?

Also when i tried to create CustomTraceListner, i'm asking to select file. what shld i select?

 

Apr 28, 2011 at 7:12 AM

Something is definitely missing here. For your convenience, I already send to your mail a sample custom database provider project

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Apr 28, 2011 at 8:24 AM

hey, Thanks for your support. I modified the files based on the requirment.

Its working.

Apr 28, 2011 at 1:46 PM
Edited Apr 28, 2011 at 7:12 PM

HI Neol,

I am very much appreciated your response.I have one more query related to logging.

I'm actually logging all exceptions into database.If I'm not able to connect the database iwant log into some rolling file or event viewr. How i can do this.

 

Can you please suggest me

Thanks & Regards,

Konda

 

 

 

Apr 29, 2011 at 2:55 AM

This behavior is not supported.  What I would suggest is to create another listener for that same log category so it will log using those listeners regardless whether one listener fails or not.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com