Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 531211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:16:37+00:00 2026-05-13T09:16:37+00:00

I’m modifying an existing winforms application to use the Logging Application Block. For historical

  • 0

I’m modifying an existing winforms application to use the Logging Application Block. For historical reasons this app gets its main database connection string from the registry, and I’d like the Logging Application Block to use the same details for logging to the database. How can I do this?

The approaches i can think of are:

1) Create a new TraceListener and implement the same sort of functionality as in FormattedDatabaseTraceListener. If I take this approach, should I inherit from CustomTraceListener, and if so how do I pass an attribute of the formatter to use?

2) Create a new ConfigurationSource that provides different details when asked for the database connection. All other requests would be passed through to a FileConfigurationSource, but when asked for the database connection details the object would read the appropriate bits from the registry instead.

but it’s not obvious which is more appropriate, or how to go about doing it. Any suggestions?

I’m using EntLib 3.1.

thanks,

-Rory

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:16:38+00:00Added an answer on May 13, 2026 at 9:16 am

    The solution I came up with:

    Initially I looked at approach (1) but it wasn’t obvious to me how to pass the AddStoredProcedureName and WriteStoredProcedureName attributes to my custom tracelistener, nor how to create a Database object since it’s normally done through factory methods that use a ConfigurationSource. (I suppose I could have used a non-Entlib db object, but I wanted to mostly copy & paste from FormattedDatabaseTraceListener rather than rewrite all the db logic).

    So the solution I’ve come to is based on (2) above. I create a new IConfigurationSource which wraps a FileConfigurationSource, but when GetSection(“connectionStrings”) is called it first populates the ConnectionStringsSection with a ConnectionStringSettings representing my custom connection string retrieved from the registry instead of from the file:

    public class PSConfigurationSource : IConfigurationSource
    {
        /// <summary>
        /// Name of the connection string that will be set to use the standard connection
        /// string from the registry. Anything wanting to reference the RM database should 
        /// reference this connection string name.
        /// </summary>
        private const string RMDatabaseName = "RMDatabase"; 
    
        private IConfigurationSource wrappedSource;
    
        private ConnectionStringsSection cxnStringsSection;
    
        /// <summary>
        /// Creates a PSConfigurationSource based on the wrappedSource.
        /// </summary>
        /// <param name="wrappedSource"></param>
        public PSConfigurationSource(IConfigurationSource wrappedSource)
        {
            this.wrappedSource = wrappedSource;
        }
    
        /// <summary>
        /// Retrieves the specified <see cref="T:System.Configuration.ConfigurationSection"/>, 
        /// unless the connectionStrings section is requested in which case our custom 
        /// config section is returned, which contains our custom connection string.
        /// </summary>
        /// <param name="sectionName">The name of the section to be retrieved.</param>
        /// <returns>
        /// The specified <see cref="T:System.Configuration.ConfigurationSection"/>, or <see langword="null"/> (<b>Nothing</b> in Visual Basic)
        ///             if a section by that name is not found.
        /// </returns>
        public ConfigurationSection GetSection(string sectionName)
        {
            if (sectionName=="connectionStrings")
            {
                EnsureConnectionStringsSectionSet();
                return cxnStringsSection;
            }
            return wrappedSource.GetSection(sectionName);
        }
    
        /// <summary>
        /// Sets the cxnStringsSection object, populating it with our standard connection 
        /// string retrieved from the registry.
        /// </summary>
        private void EnsureConnectionStringsSectionSet()
        {
            if (cxnStringsSection == null)
            {
                // Get the connectionStrings section from the config file.
                ConfigurationSection configSection = wrappedSource.GetSection("connectionStrings");
                if ((configSection != null) && (configSection is ConnectionStringsSection))
                    cxnStringsSection = configSection as ConnectionStringsSection;
                else
                    cxnStringsSection = new ConnectionStringsSection();
    
                // Add in the RM database settings. Seems that ConnectionStringSettingsCollection[<string>] doesn't have a setter, 
                //   despite it being in the documentation, so need to remove then add in case it's already there. 
                cxnStringsSection.ConnectionStrings.Remove(RMDatabaseName);
                cxnStringsSection.ConnectionStrings.Add(new ConnectionStringSettings(
                    RMDatabaseName, SomeStaticHelperClass.GetConnectionStringFromRegistry(), "System.Data.SqlClient"));
            }
        }
    
        #region WrappedMethods
    
    
        /// <summary>
        /// Adds a <see cref="T:System.Configuration.ConfigurationSection"/> to the configuration source location specified by 
        ///             <paramref name="saveParameter"/> and saves the configuration source.
        /// </summary>
        /// <remarks>
        /// If a configuration section with the specified name already exists in the location specified by 
        ///             <paramref name="saveParameter"/> it will be replaced.
        /// </remarks>
        /// <param name="saveParameter">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationParameter"/> that represents the location where 
        ///             to save the updated configuration.</param><param name="sectionName">The name by which the <paramref name="configurationSection"/> should be added.</param><param name="configurationSection">The configuration section to add.</param>
        public void Add(IConfigurationParameter saveParameter, string sectionName, ConfigurationSection configurationSection)
        {
            wrappedSource.Add(saveParameter, sectionName, configurationSection);
        }
    
        /// <summary>
        /// Removes a <see cref="T:System.Configuration.ConfigurationSection"/> from the configuration source location specified by 
        ///             <paramref name="removeParameter"/> and saves the configuration source.
        /// </summary>
        /// <param name="removeParameter">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationParameter"/> that represents the location where 
        ///             to save the updated configuration.</param><param name="sectionName">The name of the section to remove.</param>
        public void Remove(IConfigurationParameter removeParameter, string sectionName)
        {
            wrappedSource.Remove(removeParameter, sectionName);
        }
    
        /// <summary>
        /// Adds a handler to be called when changes to the section named <paramref name="sectionName"/> are detected.
        /// </summary>
        /// <param name="sectionName">The name of the section to watch for.</param><param name="handler">The handler for the change event to add.</param>
        public void AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
        {
            wrappedSource.AddSectionChangeHandler(sectionName, handler);
        }
    
        /// <summary>
        /// Removes a handler to be called when changes to section 
        /// <code>
        /// sectionName
        /// </code>
        ///  are detected.
        /// </summary>
        /// <param name="sectionName">The name of the watched section.</param><param name="handler">The handler for the change event to remove.</param>
        public void RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
        {
            wrappedSource.RemoveSectionChangeHandler(sectionName, handler);
        }
    
        #endregion  
    
    
    }
    

    I then use this ConfigurationSource instead of the default. The call to SomeStaticHelperClass.GetConnectionStringFromRegistry() gets the connection string that is used elsewhere in the app. This solution also has the advantage that I don’t need to reproduce the functionality of the FormattedDatabaseTraceListener – ie actually handling the database logic.

    To use the PSConfigurationSource instead of the default, I created a static method to get a PSConfigurationSource. In my case I put it in a class I called EnvironmentAssistant:

    public class EnvironmentAssistant
    {
    ...
            public static IConfigurationSource GetConfigurationSource()
            {
                return
                    new PSConfigurationSource(
                        new FileConfigurationSource(GetConfigFilePath()));
            }
    ...
    }
    

    Then instead of using the normal EntLib classes ExceptionPolicy and Logger (for exception handling and logging) I created new classes in my namespace for each of them. Simply copy the code for ExceptionPolicy and Logger and rename to, say, MyExceptionPolicy and MyLogger. Then make a couple of minor changes so that instead of using the default configuration source they use that static method GetConfigurationSource().

    MyExceptionPolicy (Just Initialise appears modified. I’m not sure if I added this method or just modified it. If I added it then you’ll need to add calls to it as the first line within HandleFirstException() and HandleException().)

        private static void Initialise()
        {
            if (defaultFactory == null)
            {
                // Nested check should mean that locking overhead isn't applied unless necessary, 
                // and means factory won't be overwritten if two threads hit locked section.
                lock (sync)
                {
                    if (defaultFactory == null)
                    {
                        exceptionsSource = EnvironmentAssistant.GetConfigurationSource();
                        defaultFactory = new ExceptionPolicyFactory(exceptionsSource);
                    }
                }
            }
        }
    

    MyLogger:

        private static LogWriterFactory factory = new LogWriterFactory(EnvironmentAssistant.GetConfigurationSource());
    
        ...
    
        internal static void TryLogConfigurationFailure(ConfigurationErrorsException configurationException)
        {
            try
            {
                DefaultLoggingEventLogger logger = EnterpriseLibraryFactory.BuildUp<DefaultLoggingEventLogger>(EnvironmentAssistant.GetConfigurationSource());
                logger.LogConfigurationError(configurationException);
            }
            catch
            { }
        }
    

    Then throughout my code I use MyExceptionPolicy.HandleException( ex, "policy name" ) or MyLogger.Log(...) just like you would the default EntLib classes.

    Hopefully this is helpful to someone, sometime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have found that the AXD files are necessary as… May 14, 2026 at 5:10 am
  • Editorial Team
    Editorial Team added an answer You can use css like this: overflow-y: scroll; overflow-x: hidden; May 14, 2026 at 5:10 am
  • Editorial Team
    Editorial Team added an answer Hmn had the same problem today. My alternative solution is… May 14, 2026 at 5:10 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.