~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/.svn/text-base/NDC.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
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
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
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.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
using System.Collections;
21
 
 
22
 
#if NETCF
23
 
using Stack = log4net.Util.ThreadContextStack.Stack;
24
 
#endif
25
 
 
26
 
namespace log4net
27
 
{
28
 
        /// <summary>
29
 
        /// Implementation of Nested Diagnostic Contexts.
30
 
        /// </summary>
31
 
        /// <remarks>
32
 
        /// <note>
33
 
        /// <para>
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>.
36
 
        /// </para>
37
 
        /// </note>
38
 
        /// <para>
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.
43
 
        /// </para>
44
 
        /// <para>
45
 
        /// Interleaved log output can still be meaningful if each log entry
46
 
        /// from different contexts had a distinctive stamp. This is where NDCs
47
 
        /// come into play.
48
 
        /// </para>
49
 
        /// <para>
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
52
 
        /// calling thread.
53
 
        /// </para>
54
 
        /// </remarks>
55
 
        /// <example>How to push a message into the context
56
 
        /// <code lang="C#">
57
 
        ///     using(NDC.Push("my context message"))
58
 
        ///     {
59
 
        ///             ... all log calls will have 'my context message' included ...
60
 
        ///     
61
 
        ///     } // at the end of the using block the message is automatically removed 
62
 
        /// </code>
63
 
        /// </example>
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
69
 
        {
70
 
                #region Private Instance Constructors
71
 
 
72
 
                /// <summary>
73
 
                /// Initializes a new instance of the <see cref="NDC" /> class. 
74
 
                /// </summary>
75
 
                /// <remarks>
76
 
                /// Uses a private access modifier to prevent instantiation of this class.
77
 
                /// </remarks>
78
 
                private NDC()
79
 
                {
80
 
                }
81
 
 
82
 
                #endregion Private Instance Constructors
83
 
 
84
 
                #region Public Static Properties
85
 
 
86
 
                /// <summary>
87
 
                /// Gets the current context depth.
88
 
                /// </summary>
89
 
                /// <value>The current context depth.</value>
90
 
                /// <remarks>
91
 
                /// <note>
92
 
                /// <para>
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>.
95
 
                /// </para>
96
 
                /// </note>
97
 
                /// <para>
98
 
                /// The number of context values pushed onto the context stack.
99
 
                /// </para>
100
 
                /// <para>
101
 
                /// Used to record the current depth of the context. This can then 
102
 
                /// be restored using the <see cref="SetMaxDepth"/> method.
103
 
                /// </para>
104
 
                /// </remarks>
105
 
                /// <seealso cref="SetMaxDepth"/>
106
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
107
 
                public static int Depth
108
 
                {
109
 
                        get { return ThreadContext.Stacks["NDC"].Count; }
110
 
                }
111
 
 
112
 
                #endregion Public Static Properties
113
 
 
114
 
                #region Public Static Methods
115
 
 
116
 
                /// <summary>
117
 
                /// Clears all the contextual information held on the current thread.
118
 
                /// </summary>
119
 
                /// <remarks>
120
 
                /// <note>
121
 
                /// <para>
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>.
124
 
                /// </para>
125
 
                /// </note>
126
 
                /// <para>
127
 
                /// Clears the stack of NDC data held on the current thread.
128
 
                /// </para>
129
 
                /// </remarks>
130
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
131
 
                public static void Clear() 
132
 
                {
133
 
                        ThreadContext.Stacks["NDC"].Clear();
134
 
                }
135
 
 
136
 
                /// <summary>
137
 
                /// Creates a clone of the stack of context information.
138
 
                /// </summary>
139
 
                /// <returns>A clone of the context info for this thread.</returns>
140
 
                /// <remarks>
141
 
                /// <note>
142
 
                /// <para>
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>.
145
 
                /// </para>
146
 
                /// </note>
147
 
                /// <para>
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 
150
 
                /// parent thread.
151
 
                /// </para>
152
 
                /// </remarks>
153
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
154
 
                public static Stack CloneStack() 
155
 
                {
156
 
                        return ThreadContext.Stacks["NDC"].InternalStack;
157
 
                }
158
 
 
159
 
                /// <summary>
160
 
                /// Inherits the contextual information from another thread.
161
 
                /// </summary>
162
 
                /// <param name="stack">The context stack to inherit.</param>
163
 
                /// <remarks>
164
 
                /// <note>
165
 
                /// <para>
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>.
168
 
                /// </para>
169
 
                /// </note>
170
 
                /// <para>
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
177
 
                /// this method.
178
 
                /// </para>
179
 
                /// </remarks>
180
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks", true)]*/
181
 
                public static void Inherit(Stack stack) 
182
 
                {
183
 
                        ThreadContext.Stacks["NDC"].InternalStack = stack;
184
 
                }
185
 
 
186
 
                /// <summary>
187
 
                /// Removes the top context from the stack.
188
 
                /// </summary>
189
 
                /// <returns>
190
 
                /// The message in the context that was removed from the top 
191
 
                /// of the stack.
192
 
                /// </returns>
193
 
                /// <remarks>
194
 
                /// <note>
195
 
                /// <para>
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>.
198
 
                /// </para>
199
 
                /// </note>
200
 
                /// <para>
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.
204
 
                /// </para>
205
 
                /// </remarks>
206
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
207
 
                public static string Pop() 
208
 
                {
209
 
                        return ThreadContext.Stacks["NDC"].Pop();
210
 
                }
211
 
 
212
 
                /// <summary>
213
 
                /// Pushes a new context message.
214
 
                /// </summary>
215
 
                /// <param name="message">The new context message.</param>
216
 
                /// <returns>
217
 
                /// An <see cref="IDisposable"/> that can be used to clean up 
218
 
                /// the context stack.
219
 
                /// </returns>
220
 
                /// <remarks>
221
 
                /// <note>
222
 
                /// <para>
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>.
225
 
                /// </para>
226
 
                /// </note>
227
 
                /// <para>
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
231
 
                /// context.
232
 
                /// </para>
233
 
                /// </remarks>
234
 
                /// <example>Simple example of using the <c>Push</c> method with the <c>using</c> keyword.
235
 
                /// <code lang="C#">
236
 
                /// using(log4net.NDC.Push("NDC_Message"))
237
 
                /// {
238
 
                ///             log.Warn("This should have an NDC message");
239
 
                ///     }
240
 
                /// </code>
241
 
                /// </example>
242
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
243
 
                public static IDisposable Push(string message) 
244
 
                {
245
 
                        return ThreadContext.Stacks["NDC"].Push(message);
246
 
                }
247
 
 
248
 
                /// <summary>
249
 
                /// Removes the context information for this thread. It is
250
 
                /// not required to call this method.
251
 
                /// </summary>
252
 
                /// <remarks>
253
 
                /// <note>
254
 
                /// <para>
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>.
257
 
                /// </para>
258
 
                /// </note>
259
 
                /// <para>
260
 
                /// This method is not implemented.
261
 
                /// </para>
262
 
                /// </remarks>
263
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
264
 
                public static void Remove() 
265
 
                {
266
 
                }
267
 
 
268
 
                /// <summary>
269
 
                /// Forces the stack depth to be at most <paramref name="maxDepth"/>.
270
 
                /// </summary>
271
 
                /// <param name="maxDepth">The maximum depth of the stack</param>
272
 
                /// <remarks>
273
 
                /// <note>
274
 
                /// <para>
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>.
277
 
                /// </para>
278
 
                /// </note>
279
 
                /// <para>
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.
285
 
                /// </para>
286
 
                /// </remarks>
287
 
                /*[Obsolete("NDC has been replaced by ThreadContext.Stacks")]*/
288
 
                public static void SetMaxDepth(int maxDepth) 
289
 
                {
290
 
                        if (maxDepth >= 0)
291
 
                        {
292
 
                                log4net.Util.ThreadContextStack stack = ThreadContext.Stacks["NDC"];
293
 
 
294
 
                                if (maxDepth == 0)
295
 
                                {
296
 
                                        stack.Clear();
297
 
                                }
298
 
                                else
299
 
                                {
300
 
                                        while(stack.Count > maxDepth)
301
 
                                        {
302
 
                                                stack.Pop();
303
 
                                        }
304
 
                                }
305
 
                        }
306
 
                }
307
 
 
308
 
                #endregion Public Static Methods
309
 
        }
310
 
}