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 Jonathan Beeston <jo...@ntlworld.com> on 2004/08/26 17:18:54 UTC

Application level keyed pair properties

Hello.

As for my previous post, I admit it was not a good idea.  Sorry.

I am currently developing a 'Visual Studio .NET' pluging.  However, the relative file base for these is the binary directory of 'Visual Studio .NET' itself, and not the binary director of the actual plugin when installed.  Therefore, I have found that the unrooted file properties in the configuration files seems to be relative to 'Visual Studio .NET', and not the installed plugin binary directory, as required.  I have therefore made a few minor, and I believe non-breaking', modifications.  Would it be possible to have these included at some point, if you think they are appropriate.  Thank you.

These modification consist of a small change to two files, and one new file.  These changes allow the adding of applicatoin level, as opposed to system  environment level, variables at runtime.  If set before the configuration of log4net, these keyed pair values can be used as if they were environment variables in the log4net configuration file.

The following are the changes to the two files, in unified diff format.

Index: OptionConverter.cs
===================================================================
RCS file: /home/cvspublic/logging-log4net/src/Util/OptionConverter.cs,v
retrieving revision 1.6
diff -u -r1.6 OptionConverter.cs
--- OptionConverter.cs	7 Jun 2004 00:58:44 -0000	1.6
+++ OptionConverter.cs	26 Aug 2004 14:30:59 -0000
@@ -463,7 +463,7 @@
 		/// values of keys found in <paramref name="props"/>.

 		/// </summary>

 		/// <param name="value">The string on which variable substitution is performed.</param>

-		/// <param name="props">The dictionary to use to lookup variables.</param>

+		/// <param name="props">The array of dictionaries to use to lookup variables.</param>

 		/// <remarks>

 		/// <para>

 		/// The variable substitution delimiters are <b>${</b> and <b>}</b>.

@@ -501,7 +501,7 @@
 		/// </para>

 		/// </remarks>

 		/// <returns>The result of the substitutions.</returns>

-		public static string SubstituteVariables(string value, System.Collections.IDictionary props) 

+		public static string SubstituteVariables(string value, params System.Collections.IDictionary [] props) 

 		{

 			StringBuilder buf = new StringBuilder();

 

@@ -536,12 +536,17 @@
 						j += DELIM_START_LEN;

 						string key = value.Substring(j, k - j);

 

-						string replacement = props[key] as string;

-

-						if (replacement != null) 

-						{

-							buf.Append(replacement);

-						}

+						foreach(System.Collections.IDictionary propDic in props)
+						{
+							string replacement = propDic[key] as string;
+
+							if (replacement != null) 
+							{
+								buf.Append(replacement);
+								break;
+							}
+						}
+
 						i = k + DELIM_STOP_LEN;		

 					}

 				}



Index: XmlHierarchyConfigurator.cs
===================================================================
RCS file: /home/cvspublic/logging-log4net/src/Repository/Hierarchy/XmlHierarchyConfigurator.cs,v
retrieving revision 1.8
diff -u -r1.8 XmlHierarchyConfigurator.cs
--- XmlHierarchyConfigurator.cs	19 Aug 2004 21:31:09 -0000	1.8
+++ XmlHierarchyConfigurator.cs	26 Aug 2004 14:32:54 -0000
@@ -542,7 +542,7 @@
 					try

 					{

 						// Expand environment variables in the string.

-						propertyValue = OptionConverter.SubstituteVariables(propertyValue, Environment.GetEnvironmentVariables());

+						propertyValue = OptionConverter.SubstituteVariables(propertyValue, Environment.GetEnvironmentVariables(), AppVars.Instance);

 					}

 					catch(System.Security.SecurityException)

 					{



The following is the new file:

#region Copyright & License
//
// Copyright 2001-2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections;

namespace log4net.Util
{
	/// <summary>
	/// A singleton of <see cref="System.Collections.Hashtable"/>, intended to
	/// sotre application level, as opposed to system environment level,
	/// variables.
	/// </summary>
	/// <author>Jonathan Simon Beeston</author>
#if !NETCF
	[Serializable]
#endif
	public sealed class AppVars : Hashtable
	{
		#region Private Instance Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="AppVars" /> class. 
		/// </summary>
		/// <remarks>
		/// Uses a private access modifier to enforce the singleton pattern.
		/// </remarks>
		private AppVars()
		{
		}

		#endregion Private Instance Constructors
  
		#region Public Static Properties

		/// <summary>
		/// Gets the singleton instance.
		/// </summary>
		/// <returns>The singleton instance.</returns>
		public static AppVars Instance
		{
			get
			{
				return s_instance;
			}
		}

		#endregion Public Static Properties

		#region Public Instance Methods

		/// <summary>
		/// Adds a keyed value to the singleton instance.
		/// </summary>
		/// <para>If a keyed value pair with the same key as that
		/// contained in 'key', then is ir removed before it is added.</para>
		/// <param name="key">The key for the keyed value pair.</param>
		/// <param name="value">The value for the keyed value pair.</param>
		public override void Add(object key, object value)
		{
			if(base.ContainsKey(key) == true)
			{
				base.Remove(key);
			}
			base.Add (key, value);
		}

		#endregion Public Instance Methods

		#region Private Static Fields

		/// <summary>
		/// The singleton instance of the empty collection.
		/// </summary>
		private readonly static AppVars s_instance = new AppVars();
  
		#endregion Private Static Fields
	
	}
}


Thank you.

Yours sincerely,
Jonathan Simon Beeston.


-----------------------------------------
Email provided by http://www.ntlhome.com/