You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by dp...@apache.org on 2017/04/26 09:11:50 UTC

[06/50] [abbrv] logging-log4net git commit: Added support for all old methods in NDC - Depth, SetMaxDepth, CloneStack, Inherit

Added support for all old methods in NDC - Depth, SetMaxDepth, CloneStack, Inherit



Project: http://git-wip-us.apache.org/repos/asf/logging-log4net/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4net/commit/f85c2955
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4net/tree/f85c2955
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4net/diff/f85c2955

Branch: refs/heads/origin/trunk
Commit: f85c29558ec851b22fb94388f97d9b4d3eb03a2b
Parents: 6aa2873
Author: Nicko Cadell <ni...@apache.org>
Authored: Mon Feb 14 03:45:01 2005 +0000
Committer: Nicko Cadell <ni...@apache.org>
Committed: Mon Feb 14 03:45:01 2005 +0000

----------------------------------------------------------------------
 src/NDC.cs                     | 109 +++++++++++++++++++++++++++++++-----
 src/Util/ThreadContextStack.cs |  41 +++++++++++++-
 2 files changed, 134 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4net/blob/f85c2955/src/NDC.cs
----------------------------------------------------------------------
diff --git a/src/NDC.cs b/src/NDC.cs
index aeefcf4..5b8e75e 100755
--- a/src/NDC.cs
+++ b/src/NDC.cs
@@ -25,10 +25,12 @@ namespace log4net
 	/// Implementation of Nested Diagnostic Contexts.
 	/// </summary>
 	/// <remarks>
+	/// <note>
 	/// <para>
 	/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
 	/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
 	/// </para>
+	/// </note>
 	/// <para>
 	/// A Nested Diagnostic Context, or NDC in short, is an instrument
 	/// to distinguish interleaved log output from different sources. Log
@@ -82,6 +84,12 @@ namespace log4net
 		/// </summary>
 		/// <value>The current context depth.</value>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
 		/// <para>
 		/// The number of context values pushed onto the context stack.
 		/// </para>
@@ -91,10 +99,10 @@ namespace log4net
 		/// </para>
 		/// </remarks>
 		/// <seealso cref="SetMaxDepth"/>
-		[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
 		public static int Depth
 		{
-			get { throw new NotSupportedException("NDC has been replaced by ThreadContext.Stacks"); }
+			get { return ThreadContext.Stacks["NDC"].Count; }
 		}
 
 		#endregion Public Static Properties
@@ -105,9 +113,17 @@ namespace log4net
 		/// Clears all the contextual information held on the current thread.
 		/// </summary>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// Clears the stack of NDC data held on the current thread.
+		/// </para>
 		/// </remarks>
-		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]*/
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
 		public static void Clear() 
 		{
 			ThreadContext.Stacks["NDC"].Clear();
@@ -118,14 +134,22 @@ namespace log4net
 		/// </summary>
 		/// <returns>A clone of the context info for this thread.</returns>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// The results of this method can be passed to the <see cref="Inherit"/> 
 		/// method to allow child threads to inherit the context of their 
 		/// parent thread.
+		/// </para>
 		/// </remarks>
-		[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
 		public static Stack CloneStack() 
 		{
-			throw new NotSupportedException("NDC has been replaced by ThreadContext.Stacks");
+			return ThreadContext.Stacks["NDC"].InternalStack;
 		}
 
 		/// <summary>
@@ -133,6 +157,13 @@ namespace log4net
 		/// </summary>
 		/// <param name="stack">The context stack to inherit.</param>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// This thread will use the context information from the stack
 		/// supplied. This can be used to initialize child threads with
 		/// the same contextual information as their parent threads. These
@@ -140,25 +171,34 @@ namespace log4net
 		/// are pushed onto the stack will not be visible to the other.
 		/// Call <see cref="CloneStack"/> to obtain a stack to pass to
 		/// this method.
+		/// </para>
 		/// </remarks>
-		[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]*/
 		public static void Inherit(Stack stack) 
 		{
-			throw new NotSupportedException("NDC has been replaced by ThreadContext.Stacks");
+			ThreadContext.Stacks["NDC"].InternalStack = stack;
 		}
 
 		/// <summary>
 		/// Removes the top context from the stack.
 		/// </summary>
+		/// <returns>
+		/// The message in the context that was removed from the top 
+		/// of the stack.
+		/// </returns>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// Remove the top context from the stack, and return
 		/// it to the caller. If the stack is empty then an
 		/// empty string (not <c>null</c>) is returned.
+		/// </para>
 		/// </remarks>
-		/// <returns>
-		/// The message in the context that was removed from the top 
-		/// of the stack.
-		/// </returns>
 		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
 		public static string Pop() 
 		{
@@ -174,10 +214,18 @@ namespace log4net
 		/// the context stack.
 		/// </returns>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// Pushes a new context onto the context stack. An <see cref="IDisposable"/>
 		/// is returned that can be used to clean up the context stack. This
 		/// can be easily combined with the <c>using</c> keyword to scope the
 		/// context.
+		/// </para>
 		/// </remarks>
 		/// <example>Simple example of using the <c>Push</c> method with the <c>using</c> keyword.
 		/// <code lang="C#">
@@ -198,9 +246,17 @@ namespace log4net
 		/// not required to call this method.
 		/// </summary>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// This method is not implemented.
+		/// </para>
 		/// </remarks>
-		[Obsolete("NDC has been replaced by ThreadContext.Stacks")]
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
 		public static void Remove() 
 		{
 		}
@@ -210,16 +266,39 @@ namespace log4net
 		/// </summary>
 		/// <param name="maxDepth">The maximum depth of the stack</param>
 		/// <remarks>
+		/// <note>
+		/// <para>
+		/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
+		/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
+		/// </para>
+		/// </note>
+		/// <para>
 		/// Forces the stack depth to be at most <paramref name="maxDepth"/>.
 		/// This may truncate the head of the stack. This only affects the 
 		/// stack in the current thread. Also it does not prevent it from
 		/// growing, it only sets the maximum depth at the time of the
 		/// call. This can be used to return to a known context depth.
+		/// </para>
 		/// </remarks>
-		[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]
-		static public void SetMaxDepth(int maxDepth) 
+		/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
+		public static void SetMaxDepth(int maxDepth) 
 		{
-			throw new NotSupportedException("NDC has been replaced by ThreadContext.Stacks");
+			if (maxDepth >= 0)
+			{
+				log4net.Util.ThreadContextStack stack = ThreadContext.Stacks["NDC"];
+
+				if (maxDepth == 0)
+				{
+					stack.Clear();
+				}
+				else
+				{
+					while(stack.Count > maxDepth)
+					{
+						stack.Pop();
+					}
+				}
+			}
 		}
 
 		#endregion Public Static Methods

http://git-wip-us.apache.org/repos/asf/logging-log4net/blob/f85c2955/src/Util/ThreadContextStack.cs
----------------------------------------------------------------------
diff --git a/src/Util/ThreadContextStack.cs b/src/Util/ThreadContextStack.cs
index 4840268..8bf5c0b 100755
--- a/src/Util/ThreadContextStack.cs
+++ b/src/Util/ThreadContextStack.cs
@@ -42,7 +42,7 @@ namespace log4net.Util
 		/// <summary>
 		/// The stack store.
 		/// </summary>
-		private readonly Stack m_stack = new Stack();
+		private Stack m_stack = new Stack();
 
 		#endregion Private Static Fields
 
@@ -62,6 +62,28 @@ namespace log4net.Util
 
 		#endregion Public Instance Constructors
 
+		#region Public Properties
+
+		/// <summary>
+		/// The number of messages in the stack
+		/// </summary>
+		/// <value>
+		/// The current number of messages in the stack
+		/// </value>
+		/// <remarks>
+		/// <para>
+		/// The current number of messages in the stack. That is
+		/// the number of times <see cref="Push"/> has been called
+		/// minus the number of times <see cref="Pop"/> has been called.
+		/// </para>
+		/// </remarks>
+		public int Count
+		{
+			get { return m_stack.Count; }
+		}
+
+		#endregion // Public Properties
+
 		#region Public Methods
 
 		/// <summary>
@@ -153,6 +175,23 @@ namespace log4net.Util
 			}
 			return null;
 		}
+
+		/// <summary>
+		/// Gets and sets the internal stack used by this <see cref="ThreadContextStack"/>
+		/// </summary>
+		/// <value>The internal storage stack</value>
+		/// <remarks>
+		/// <para>
+		/// This property is provided only to support backward compatability 
+		/// of the <see cref="NDC"/>. Tytpically the internal stack should not
+		/// be modified.
+		/// </para>
+		/// </remarks>
+		internal Stack InternalStack
+		{
+			get { return m_stack; }
+			set { m_stack = value; }
+		}
   
 		#endregion Internal Methods