Пример #1
0
        /// <summary>
        /// Forces the stack depth to be at most <paramref name="maxDepth"/>.
        /// </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")]*/
        public static void SetMaxDepth(int maxDepth)
        {
            if (maxDepth >= 0)
            {
                Util.ThreadContextStack stack = ThreadContext.Stacks["NDC"];

                if (maxDepth == 0)
                {
                    stack.Clear();
                }
                else
                {
                    while (stack.Count > maxDepth)
                    {
                        stack.Pop();
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the named thread context stack
        /// </summary>
        /// <value>
        /// The named stack
        /// </value>
        /// <remarks>
        /// <para>
        /// Gets the named thread context stack
        /// </para>
        /// </remarks>
        public ThreadContextStack this[string key]
        {
            get
            {
                ThreadContextStack stack = null;

                object propertyValue = m_properties[key];
                if (propertyValue == null)
                {
                    // Stack does not exist, create
                    stack             = new ThreadContextStack();
                    m_properties[key] = stack;
                }
                else
                {
                    // Look for existing stack
                    stack = propertyValue as ThreadContextStack;
                    if (stack == null)
                    {
                        // Property is not set to a stack!
                        string propertyValueString = SystemInfo.NullText;

                        try
                        {
                            propertyValueString = propertyValue.ToString();
                        }
                        catch
                        {
                        }

                        LogLog.Error(declaringType, "ThreadContextStacks: Request for stack named [" + key + "] failed because a property with the same name exists which is a [" + propertyValue.GetType().Name + "] with value [" + propertyValueString + "]");

                        stack = new ThreadContextStack();
                    }
                }

                return(stack);
            }
        }