--- DefaultRepositorySelector.cs.orig 2005-01-17 19:18:42.000000000 -0700 +++ DefaultRepositorySelector.cs 2005-06-10 07:57:44.152676700 -0600 @@ -1,6 +1,7 @@ #region Copyright & License // // Copyright 2001-2005 The Apache Software Foundation +// Copyright (C) 2005 Hewlett-Packard Development Company, L.P. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -42,6 +43,7 @@ /// /// Nicko Cadell /// Gert Driesen + /// Patrick Conant public class DefaultRepositorySelector : IRepositorySelector { #region Public Events @@ -625,17 +627,61 @@ throw new ArgumentNullException("repository"); } - // Look for the Configurator attributes (e.g. DOMConfiguratorAttribute) on the assembly - object[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.ConfiguratorAttribute), false); - if (configAttributes != null && configAttributes.Length > 0) + // If 'repository' is the default repository and the app setting is available, + // and the app settings don't indicate that we should skip the default init + // process, then use the app settings to configure the repository. + bool repositoryConfigured = false; + string defaultInitOverride = System.Configuration.ConfigurationSettings.AppSettings.Get(DEFAULT_INIT_OVERRIDE_KEY); + + if (repository.Name.Equals(DefaultRepositoryName) && (defaultInitOverride == null || "false".Equals(defaultInitOverride.ToLower()))) { - // Sort the ConfiguratorAttributes in priority order - Array.Sort(configAttributes); + string configurationOptionStr = System.Configuration.ConfigurationSettings.AppSettings.Get(DEFAULT_CONFIGURATION_KEY); + Uri uri = null; + + try + { + uri = new Uri(configurationOptionStr); + } + catch(UriFormatException ex) + { + // The Uri appears invalid, so don't use it. + if (log4net.Util.LogLog.IsDebugEnabled) + log4net.Util.LogLog.Debug("DefaultRepositorySelector: Invalid value for " + DEFAULT_CONFIGURATION_KEY + " configuration key. [" + configurationOptionStr + "] is not a valid URI. " + ex.Message); + uri = null; + } - // Delegate to the attribute the job of configuring the repository - foreach(log4net.Config.ConfiguratorAttribute configAttr in configAttributes) + // If we have a non-null url, then delegate the rest of the + // configuration to the Configurator. + if(uri != null) { - configAttr.Configure(assembly, repository); + if (log4net.Util.LogLog.IsDebugEnabled) + log4net.Util.LogLog.Debug("DefaultRepositorySelector: Using URI [" + uri + "] for automatic log4net configuration."); + log4net.Config.XmlConfigurator.Configure(repository, uri); + repositoryConfigured = true; + } + else + { + log4net.Util.LogLog.Debug("DefaultRepositorySelector: Could not find resource: [" + configurationOptionStr + "]."); + } + } + + // Otherwise, this isn't the default repository, the app setting isn't available (or valid) + // or the default init process has been over-ridden. In this case, use the attribute-based + // initialization process. + if (!repositoryConfigured) + { + // Look for the Configurator attributes (e.g. DOMConfiguratorAttribute) on the assembly + object[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(log4net.Config.ConfiguratorAttribute), false); + if (configAttributes != null && configAttributes.Length > 0) + { + // Sort the ConfiguratorAttributes in priority order + Array.Sort(configAttributes); + + // Delegate to the attribute the job of configuring the repository + foreach(log4net.Config.ConfiguratorAttribute configAttr in configAttributes) + { + configAttr.Configure(assembly, repository); + } } } } @@ -725,6 +771,17 @@ private const string DefaultRepositoryName = "log4net-default-repository"; + /// + /// Private key used internally to discover logging configuration. + /// + private static readonly string DEFAULT_CONFIGURATION_KEY = "log4net.configuration"; + + /// + /// Private key used internally, allows over-ride of default logging configuration. + /// + private static readonly string DEFAULT_INIT_OVERRIDE_KEY = "log4net.defaultInitOverride"; + + #endregion Private Static Fields #region Private Instance Fields