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 2004/09/13 09:53:39 UTC

cvs commit: logging-log4net/src/Util PatternString.cs

nicko       2004/09/13 00:53:39

  Modified:    src/Util PatternString.cs
  Log:
  Added support for adding new pattern converters during configuration
  
  Revision  Changes    Path
  1.8       +108 -7    logging-log4net/src/Util/PatternString.cs
  
  Index: PatternString.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/src/Util/PatternString.cs,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PatternString.cs	9 Sep 2004 21:53:14 -0000	1.7
  +++ PatternString.cs	13 Sep 2004 07:53:39 -0000	1.8
  @@ -40,7 +40,7 @@
   	/// of the process in general.</para>
   	/// </remarks>
   	/// <author>Nicko Cadell</author>
  -	public class PatternString
  +	public class PatternString : IOptionHandler
   	{
   		#region Static Fields
   
  @@ -63,6 +63,11 @@
   		/// </summary>
   		private PatternConverter m_head;
   
  +		/// <summary>
  +		/// patterns defined on this PatternString only
  +		/// </summary>
  +		private Hashtable m_instanceRulesRegistry = new Hashtable();
  +
   		#endregion
   
   		#region Static Constructor
  @@ -105,7 +110,8 @@
   		/// <param name="pattern">The pattern to use with this PatternString</param>
   		public PatternString(string pattern)
   		{
  -			ConversionPattern = pattern;
  +			m_pattern = pattern;
  +			ActivateOptions();
   		}
   
   		#endregion
  @@ -118,13 +124,34 @@
   		public string ConversionPattern
   		{
   			get { return m_pattern;	}
  -			set
  -			{
  -				m_pattern = value;
  -				m_head = CreatePatternParser(m_pattern).Parse();
  -			}
  +			set { m_pattern = value; }
   		}
   
  +		#region Implementation of IOptionHandler
  +
  +		/// <summary>
  +		/// Initialize object options
  +		/// </summary>
  +		/// <remarks>
  +		/// <para>
  +		/// This is part of the <see cref="IOptionHandler"/> delayed object
  +		/// activation scheme. The <see cref="ActivateOptions"/> method must 
  +		/// be called on this object after the configuration properties have
  +		/// been set. Until <see cref="ActivateOptions"/> is called this
  +		/// object is in an undefined state and must not be used. 
  +		/// </para>
  +		/// <para>
  +		/// If any of the configuration properties are modified then 
  +		/// <see cref="ActivateOptions"/> must be called again.
  +		/// </para>
  +		/// </remarks>
  +		virtual public void ActivateOptions() 
  +		{
  +			m_head = CreatePatternParser(m_pattern).Parse();
  +		}
  +
  +		#endregion
  +
   		/// <summary>
   		/// Returns PatternParser used to parse the conversion string. Subclasses
   		/// may override this to return a subclass of PatternParser which recognize
  @@ -141,6 +168,11 @@
   			{
   				patternParser.PatternConverters.Add(entry.Key, entry.Value);
   			}
  +			// Add the instance patterns
  +			foreach(DictionaryEntry entry in m_instanceRulesRegistry)
  +			{
  +				patternParser.PatternConverters[entry.Key] = entry.Value;
  +			}
   
   			return patternParser;
   		}
  @@ -151,6 +183,11 @@
   		/// <param name="writer">The TextWriter to write the formatted event to</param>
   		public void Format(TextWriter writer) 
   		{
  +			if (writer == null)
  +			{
  +				throw new ArgumentNullException("writer");
  +			}
  +
   			PatternConverter c = m_head;
   
   			// loop through the chain of pattern converters
  @@ -170,6 +207,70 @@
   			StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
   			Format(writer);
   			return writer.ToString();
  +		}
  +
  +		/// <summary>
  +		/// Add a converter to this PatternString
  +		/// </summary>
  +		/// <param name="converterInfo">the converter info</param>
  +		/// <remarks>
  +		/// This version of the method is used by the configurator.
  +		/// Programmatic users should use the alternative <see cref="AddConverter(string,Type)"/> method.
  +		/// </remarks>
  +		public void AddConverter(ConverterInfo converterInfo)
  +		{
  +			AddConverter(converterInfo.Name, converterInfo.Type);
  +		}
  +
  +		/// <summary>
  +		/// Add a converter to this PatternString
  +		/// </summary>
  +		/// <param name="name">the name of the conversion pattern for this converter</param>
  +		/// <param name="type">the type of the converter</param>
  +		public void AddConverter(string name, Type type)
  +		{
  +			if (name == null) throw new ArgumentNullException("name");
  +			if (type == null) throw new ArgumentNullException("type");
  +
  +			if (!typeof(PatternConverter).IsAssignableFrom(type))
  +			{
  +				throw new ArgumentException("The converter type specified ["+type+"] must be a subclass of log4net.Util.PatternConverter", "type");
  +			}
  +			m_instanceRulesRegistry[name] = type;
  +		}
  +
  +		/// <summary>
  +		/// Wrapper class used to map converter names to converter types
  +		/// </summary>
  +		public sealed class ConverterInfo
  +		{
  +			private string m_name;
  +			private Type m_type;
  +
  +			/// <summary>
  +			/// default constructor
  +			/// </summary>
  +			public ConverterInfo()
  +			{
  +			}
  +
  +			/// <summary>
  +			/// Gets or sets the name of the conversion pattern
  +			/// </summary>
  +			public string Name
  +			{
  +				get { return m_name; }
  +				set { m_name = value; }
  +			}
  +
  +			/// <summary>
  +			/// Gets or sets the type of the converter
  +			/// </summary>
  +			public Type Type
  +			{
  +				get { return m_type; }
  +				set { m_type = value; }
  +			}
   		}
   	}
   }