예제 #1
0
        /// <summary>
        /// Renders the specified Nested Diagnostics Context item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            var messages = NestedDiagnosticsContext.GetAllObjects();

            int startPos = 0;
            int endPos   = messages.Length;

            if (TopFrames != -1)
            {
                endPos = Math.Min(TopFrames, messages.Length);
            }
            else if (BottomFrames != -1)
            {
                startPos = messages.Length - Math.Min(BottomFrames, messages.Length);
            }

            string currentSeparator = string.Empty;

            for (int i = endPos - 1; i >= startPos; --i)
            {
                var stringValue = Internal.FormatHelper.ConvertToString(messages[i], logEvent.FormatProvider);
                builder.Append(currentSeparator);
                builder.Append(stringValue);
                currentSeparator = Separator;
            }
        }
예제 #2
0
        /// <summary>
        /// Renders the specified Nested Diagnostics Context item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            if (TopFrames == 1)
            {
                // Allows fast rendering of topframes=1
                var topFrame = NestedDiagnosticsContext.PeekObject();
                if (topFrame != null)
                {
                    AppendAsString(topFrame, GetFormatProvider(logEvent), builder);
                }
                return;
            }

            var messages = NestedDiagnosticsContext.GetAllObjects();

            if (messages.Length == 0)
            {
                return;
            }

            int startPos = 0;
            int endPos   = messages.Length;

            if (TopFrames != -1)
            {
                endPos = Math.Min(TopFrames, messages.Length);
            }
            else if (BottomFrames != -1)
            {
                startPos = messages.Length - Math.Min(BottomFrames, messages.Length);
            }

            var    formatProvider   = GetFormatProvider(logEvent);
            string currentSeparator = string.Empty;

            for (int i = endPos - 1; i >= startPos; --i)
            {
                builder.Append(currentSeparator);
                AppendAsString(messages[i], formatProvider, builder);
                currentSeparator = Separator;
            }
        }
        public void NDCTest2_object()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 100;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        NestedDiagnosticsContext.Clear();
                        Assert.Null(NestedDiagnosticsContext.TopObject);
                        Assert.Null(NestedDiagnosticsContext.PopObject());
                        AssertContents(NestedDiagnosticsContext.GetAllMessages());
                        using (NestedDiagnosticsContext.Push("foo"))
                        {
                            Assert.Equal("foo", NestedDiagnosticsContext.TopObject);
                            AssertContents(NestedDiagnosticsContext.GetAllObjects(), "foo");
                            using (NestedDiagnosticsContext.Push("bar"))
                            {
                                AssertContents(NestedDiagnosticsContext.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NestedDiagnosticsContext.TopObject);
                                NestedDiagnosticsContext.Push("baz");
                                AssertContents(NestedDiagnosticsContext.GetAllObjects(), "baz", "bar", "foo");
                                Assert.Equal("baz", NestedDiagnosticsContext.TopObject);
                                Assert.Equal("baz", NestedDiagnosticsContext.PopObject());

                                AssertContents(NestedDiagnosticsContext.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NestedDiagnosticsContext.TopObject);
                            }

                            AssertContents(NestedDiagnosticsContext.GetAllObjects(), "foo");
                            Assert.Equal("foo", NestedDiagnosticsContext.TopObject);
                        }

                        AssertContents(NestedDiagnosticsContext.GetAllMessages());
                        Assert.Null(NestedDiagnosticsContext.PopObject());
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            StringBuilder exceptionsMessage = new StringBuilder();

            foreach (var ex in exceptions)
            {
                if (exceptionsMessage.Length > 0)
                {
                    exceptionsMessage.Append("\r\n");
                }

                exceptionsMessage.Append(ex.ToString());
            }

            Assert.True(exceptions.Count == 0, exceptionsMessage.ToString());
        }