예제 #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)
        {
            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;
            }
        }