You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by ni...@apache.org on 2005/02/14 04:12:39 UTC

cvs commit: logging-log4net/src/Config XmlConfigurator.cs

nicko       2005/02/13 19:12:39

  Modified:    src/Config XmlConfigurator.cs
  Log:
  Added support for loading the configuration data via a URI
  
  Revision  Changes    Path
  1.13      +93 -1     logging-log4net/src/Config/XmlConfigurator.cs
  
  Index: XmlConfigurator.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/src/Config/XmlConfigurator.cs,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- XmlConfigurator.cs	17 Jan 2005 20:18:42 -0000	1.12
  +++ XmlConfigurator.cs	14 Feb 2005 03:12:39 -0000	1.13
  @@ -22,6 +22,7 @@
   using System.IO;
   using System.Reflection;
   using System.Threading;
  +using System.Net;
   
   using log4net.Appender;
   using log4net.Util;
  @@ -306,7 +307,26 @@
   		}
   
   		/// <summary>
  -		/// Configures log4net using the specified configuration file.
  +		/// Configures log4net using the specified configuration URI.
  +		/// </summary>
  +		/// <param name="configUri">A URI to load the XML configuration from.</param>
  +		/// <remarks>
  +		/// <para>
  +		/// The configuration data must be valid XML. It must contain
  +		/// at least one element called <c>log4net</c> that holds
  +		/// the log4net configuration data.
  +		/// </para>
  +		/// <para>
  +		/// The <see cref="System.Net.WebRequest"/> must support the URI scheme specified.
  +		/// </para>
  +		/// </remarks>
  +		static public void Configure(Uri configUri)
  +		{
  +			Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()), configUri);
  +		}
  +
  +		/// <summary>
  +		/// Configures log4net using the specified configuration data stream.
   		/// </summary>
   		/// <param name="configStream">A stream to load the XML configuration from.</param>
   		/// <remarks>
  @@ -474,6 +494,78 @@
   			}
   		}
   
  +		/// <summary>
  +		/// Configures the <see cref="ILoggerRepository"/> using the specified configuration 
  +		/// URI.
  +		/// </summary>
  +		/// <param name="repository">The repository to configure.</param>
  +		/// <param name="configUri">A URI to load the XML configuration from.</param>
  +		/// <remarks>
  +		/// <para>
  +		/// The configuration data must be valid XML. It must contain
  +		/// at least one element called <c>log4net</c> that holds
  +		/// the configuration data.
  +		/// </para>
  +		/// <para>
  +		/// The <see cref="System.Net.WebRequest"/> must support the URI scheme specified.
  +		/// </para>
  +		/// </remarks>
  +		static public void Configure(ILoggerRepository repository, Uri configUri)
  +		{
  +			LogLog.Debug("XmlConfigurator: configuring repository [" + repository.Name + "] using URI ["+configUri+"]");
  +
  +			if (configUri == null)
  +			{
  +				LogLog.Error("XmlConfigurator: Configure called with null 'configUri' parameter");
  +			}
  +			else
  +			{
  +				if (configUri.IsFile)
  +				{
  +					// If URI is local file then call Configure with FileInfo
  +					Configure(repository, new FileInfo(configUri.LocalPath));
  +				}
  +				else
  +				{
  +					WebRequest configRequest = null;
  +
  +					try
  +					{
  +						configRequest = WebRequest.Create(configUri);
  +					}
  +					catch(Exception ex)
  +					{
  +						LogLog.Error("XmlConfigurator: Failed to create WebRequest for URI ["+configUri+"]", ex);
  +					}
  +
  +					if (configRequest != null)
  +					{
  +						try
  +						{
  +							WebResponse response = configRequest.GetResponse();
  +							if (response != null)
  +							{
  +								try
  +								{
  +									using(Stream configStream = response.GetResponseStream())
  +									{
  +										Configure(repository, configStream);
  +									}
  +								}
  +								finally
  +								{
  +									response.Close();
  +								}
  +							}
  +						}
  +						catch(Exception ex)
  +						{
  +							LogLog.Error("XmlConfigurator: Failed to request config from URI ["+configUri+"]", ex);
  +						}
  +					}
  +				}
  +			}
  +		}
   
   		/// <summary>
   		/// Configures the <see cref="ILoggerRepository"/> using the specified configuration