1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
20
using System.Collections;
23
using Stack = log4net.Util.ThreadContextStack.Stack;
29
/// Implementation of Nested Diagnostic Contexts.
34
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
35
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
39
/// A Nested Diagnostic Context, or NDC in short, is an instrument
40
/// to distinguish interleaved log output from different sources. Log
41
/// output is typically interleaved when a server handles multiple
42
/// clients near-simultaneously.
45
/// Interleaved log output can still be meaningful if each log entry
46
/// from different contexts had a distinctive stamp. This is where NDCs
50
/// Note that NDCs are managed on a per thread basis. The NDC class
51
/// is made up of static methods that operate on the context of the
55
/// <example>How to push a message into the context
57
/// using(NDC.Push("my context message"))
59
/// ... all log calls will have 'my context message' included ...
61
/// } // at the end of the using block the message is automatically removed
64
/// <threadsafety static="true" instance="true" />
65
/// <author>Nicko Cadell</author>
66
/// <author>Gert Driesen</author>
67
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
68
public sealed class NDC
70
#region Private Instance Constructors
73
/// Initializes a new instance of the <see cref="NDC" /> class.
76
/// Uses a private access modifier to prevent instantiation of this class.
82
#endregion Private Instance Constructors
84
#region Public Static Properties
87
/// Gets the current context depth.
89
/// <value>The current context depth.</value>
93
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
94
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
98
/// The number of context values pushed onto the context stack.
101
/// Used to record the current depth of the context. This can then
102
/// be restored using the <see cref="SetMaxDepth"/> method.
105
/// <seealso cref="SetMaxDepth"/>
106
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
107
public static int Depth
109
get { return ThreadContext.Stacks["NDC"].Count; }
112
#endregion Public Static Properties
114
#region Public Static Methods
117
/// Clears all the contextual information held on the current thread.
122
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
123
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
127
/// Clears the stack of NDC data held on the current thread.
130
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
131
public static void Clear()
133
ThreadContext.Stacks["NDC"].Clear();
137
/// Creates a clone of the stack of context information.
139
/// <returns>A clone of the context info for this thread.</returns>
143
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
144
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
148
/// The results of this method can be passed to the <see cref="Inherit"/>
149
/// method to allow child threads to inherit the context of their
153
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
154
public static Stack CloneStack()
156
return ThreadContext.Stacks["NDC"].InternalStack;
160
/// Inherits the contextual information from another thread.
162
/// <param name="stack">The context stack to inherit.</param>
166
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
167
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
171
/// This thread will use the context information from the stack
172
/// supplied. This can be used to initialize child threads with
173
/// the same contextual information as their parent threads. These
174
/// contexts will <b>NOT</b> be shared. Any further contexts that
175
/// are pushed onto the stack will not be visible to the other.
176
/// Call <see cref="CloneStack"/> to obtain a stack to pass to
180
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]*/
181
public static void Inherit(Stack stack)
183
ThreadContext.Stacks["NDC"].InternalStack = stack;
187
/// Removes the top context from the stack.
190
/// The message in the context that was removed from the top
196
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
197
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
201
/// Remove the top context from the stack, and return
202
/// it to the caller. If the stack is empty then an
203
/// empty string (not <c>null</c>) is returned.
206
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
207
public static string Pop()
209
return ThreadContext.Stacks["NDC"].Pop();
213
/// Pushes a new context message.
215
/// <param name="message">The new context message.</param>
217
/// An <see cref="IDisposable"/> that can be used to clean up
218
/// the context stack.
223
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
224
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
228
/// Pushes a new context onto the context stack. An <see cref="IDisposable"/>
229
/// is returned that can be used to clean up the context stack. This
230
/// can be easily combined with the <c>using</c> keyword to scope the
234
/// <example>Simple example of using the <c>Push</c> method with the <c>using</c> keyword.
236
/// using(log4net.NDC.Push("NDC_Message"))
238
/// log.Warn("This should have an NDC message");
242
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
243
public static IDisposable Push(string message)
245
return ThreadContext.Stacks["NDC"].Push(message);
249
/// Removes the context information for this thread. It is
250
/// not required to call this method.
255
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
256
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
260
/// This method is not implemented.
263
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
264
public static void Remove()
269
/// Forces the stack depth to be at most <paramref name="maxDepth"/>.
271
/// <param name="maxDepth">The maximum depth of the stack</param>
275
/// The NDC is deprecated and has been replaced by the <see cref="ThreadContext.Stacks"/>.
276
/// The current NDC implementation forwards to the <c>ThreadContext.Stacks["NDC"]</c>.
280
/// Forces the stack depth to be at most <paramref name="maxDepth"/>.
281
/// This may truncate the head of the stack. This only affects the
282
/// stack in the current thread. Also it does not prevent it from
283
/// growing, it only sets the maximum depth at the time of the
284
/// call. This can be used to return to a known context depth.
287
/*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
288
public static void SetMaxDepth(int maxDepth)
292
log4net.Util.ThreadContextStack stack = ThreadContext.Stacks["NDC"];
300
while(stack.Count > maxDepth)
308
#endregion Public Static Methods