Exemplo n.º 1
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (this.IsFixedText)
            {
                return(this.fixedText);
            }

            // Only makes sense to cache message if layouts are not thread agnostic
            if (!this.IsThreadAgnostic)
            {
                string cachedValue;

                if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
                {
                    return(cachedValue);
                }
            }

            int initialSize = this.maxRenderedLength;

            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }
            string result;

            if (this.LoggingConfiguration.PoolingEnabled())
            {
                var pool    = this.LoggingConfiguration.PoolFactory.Get <StringBuilderPool, StringBuilder>();
                var builder = pool.Get();
                this.Render(builder, logEvent);

                result = builder.ToString();
                pool.PutBack(builder);
            }
            else
            {
                var builder = new StringBuilder(initialSize);
                this.Render(builder, logEvent);
                result = builder.ToString();
            }

            if (!this.IsThreadAgnostic)
            {
                if (logEvent.TryGetCachedLayoutValue(this, out result))
                {
                    return(result);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (IsFixedText)
            {
                return(this.fixedText);
            }

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            int initialSize = this.maxRenderedLength;

            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }

            var builder = new StringBuilder(initialSize);

            RenderAllRenderers(logEvent, builder);
            if (builder.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = builder.Length;
            }

            return(logEvent.AddCachedLayoutValue(this, builder.ToString()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Renders the event info in layout.
        /// </summary>
        /// <param name="layout">The layout.</param>
        /// <param name="logEvent">The event info.</param>
        /// <returns>String representing log event.</returns>
        protected string RenderLogEvent(Layout layout, LogEventInfo logEvent)
        {
            if (OptimizeBufferReuse)
            {
                SimpleLayout simpleLayout = layout as SimpleLayout;
                if (simpleLayout != null && simpleLayout.IsFixedText)
                {
                    return(simpleLayout.Render(logEvent));
                }

                if (!layout.ThreadAgnostic)
                {
                    if (logEvent.TryGetCachedLayoutValue(layout, out var value))
                    {
                        return(value?.ToString() ?? string.Empty);
                    }
                }

                using (var localTarget = ReusableLayoutBuilder.Allocate())
                {
                    return(layout.RenderAllocateBuilder(logEvent, localTarget.Result));
                }
            }
            else
            {
                return(layout.Render(logEvent));
            }
        }
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (IsFixedText)
            {
                return(this.fixedText);
            }

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            int initialSize = this.maxRenderedLength;

            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }

            var builder = new StringBuilder(initialSize);

            //Memory profiling pointed out that using a foreach-loop was allocating
            //an Enumerator. Switching to a for-loop avoids the memory allocation.
            for (int i = 0; i < this.Renderers.Count; i++)
            {
                LayoutRenderer renderer = this.Renderers[i];
                try
                {
                    renderer.Render(builder, logEvent);
                }
                catch (Exception exception)
                {
                    //also check IsErrorEnabled, otherwise 'MustBeRethrown' writes it to Error

                    //check for performance
                    if (InternalLogger.IsWarnEnabled || InternalLogger.IsErrorEnabled)
                    {
                        InternalLogger.Warn(exception, "Exception in '{0}.Append()'", renderer.GetType().FullName);
                    }

                    if (exception.MustBeRethrown())
                    {
                        throw;
                    }
                }
            }

            if (builder.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = builder.Length;
            }

            string value = builder.ToString();

            logEvent.AddCachedLayoutValue(this, value);
            return(value);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return cachedValue;
            }

            return logEvent.AddCachedLayoutValue(this, this.Renderer.Render(logEvent));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            return(logEvent.AddCachedLayoutValue(this, this.Renderer.Render(logEvent)));
        }
Exemplo n.º 7
0
Arquivo: Target.cs Projeto: zmidl/NLog
        private static bool TryGetCachedValue(Layout layout, LogEventInfo logEvent, out string value)
        {
            if ((!layout.ThreadAgnostic || layout.MutableUnsafe) && logEvent.TryGetCachedLayoutValue(layout, out var value2))
            {
                value = value2?.ToString() ?? string.Empty;
                return(true);
            }

            value = null;
            return(false);
        }
Exemplo n.º 8
0
            /// <summary>
            /// Renders the layout for the specified logging event by invoking layout renderers.
            /// </summary>
            /// <param name="logEvent">The logging event.</param>
            /// <returns>The rendered layout.</returns>
            protected override string GetFormattedMessage(LogEventInfo logEvent)
            {
                string cached;

                if (logEvent.TryGetCachedLayoutValue(this, out cached))
                {
                    return(cached);
                }

                return(logEvent.AddCachedLayoutValue(this, this.parent.GetHeader()));
            }
Exemplo n.º 9
0
        private void Render(StringBuilder target, LogEventInfo logEvent)
        {
            if (!this.IsThreadAgnostic)
            {
                string cachedValue;

                if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
                {
                    target.Append(cachedValue);
                    return;
                }
            }


            StringBuilder intermediate = target;

            if (target.Length != 0)
            {
                int initialSize = this.maxRenderedLength;
                if (initialSize > MaxInitialRenderBufferLength)
                {
                    initialSize = MaxInitialRenderBufferLength;
                }

                if (this.LoggingConfiguration.PoolingEnabled())
                {
                    intermediate = this.LoggingConfiguration.PoolFactory.Get <StringBuilderPool, StringBuilder>().Get();
                }
                else
                {
                    intermediate = new StringBuilder(initialSize);
                }
            }

            this.RenderAllRenderers(logEvent, intermediate);

            if (!this.IsThreadAgnostic)
            {
                logEvent.AddCachedLayoutValue(this, intermediate.ToString());
            }

            if (intermediate.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = target.Length;
            }

            if (target != intermediate)
            {
                target.Append(intermediate);

                this.LoggingConfiguration.PutBack(intermediate);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (IsFixedText)
            {
                return(this.fixedText);
            }

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            int initialSize = this.maxRenderedLength;

            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }

            var builder = new StringBuilder(initialSize);

            foreach (LayoutRenderer renderer in this.Renderers)
            {
                try
                {
                    renderer.Render(builder, logEvent);
                }
                catch (Exception exception)
                {
                    if (exception.MustBeRethrown())
                    {
                        throw;
                    }

                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.Append(): {1}.", renderer.GetType().FullName, exception);
                    }
                }
            }

            if (builder.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = builder.Length;
            }

            string value = builder.ToString();

            logEvent.AddCachedLayoutValue(this, value);
            return(value);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Render the raw filename from Layout
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        /// <param name="reusableBuilder">StringBuilder to minimize allocations [optional].</param>
        /// <returns>String representation of a layout.</returns>
        private string GetRenderedFileName(LogEventInfo logEvent, System.Text.StringBuilder reusableBuilder = null)
        {
            if (_cleanedFixedResult != null)
            {
                return(_cleanedFixedResult);
            }

            if (_layout == null)
            {
                return(null);
            }

            if (reusableBuilder != null)
            {
                if (!_layout.ThreadAgnostic)
                {
                    object cachedResult;
                    if (logEvent.TryGetCachedLayoutValue(_layout, out cachedResult))
                    {
                        return(cachedResult?.ToString() ?? string.Empty);
                    }
                }

                _layout.RenderAppendBuilder(logEvent, reusableBuilder);

                if (_cachedPrevRawFileName != null && _cachedPrevRawFileName.Length == reusableBuilder.Length)
                {
                    // If old filename matches the newly rendered, then no need to call StringBuilder.ToString()
                    for (int i = 0; i < _cachedPrevRawFileName.Length; ++i)
                    {
                        if (_cachedPrevRawFileName[i] != reusableBuilder[i])
                        {
                            _cachedPrevRawFileName = null;
                            break;
                        }
                    }
                    if (_cachedPrevRawFileName != null)
                    {
                        return(_cachedPrevRawFileName);
                    }
                }

                _cachedPrevRawFileName   = reusableBuilder.ToString();
                _cachedPrevCleanFileName = null;
                return(_cachedPrevRawFileName);
            }
            else
            {
                return(_layout.Render(logEvent));
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the bytes to be written.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        /// <returns>Byte array.</returns>
        protected virtual byte[] GetBytesToWrite(LogEventInfo logEvent)
        {
            if (OptimizeBufferReuse)
            {
                if (!NewLine && logEvent.TryGetCachedLayoutValue(Layout, out var text))
                {
                    InternalLogger.Trace("{0} - Sending {1}", this, text);
                    return(Encoding.GetBytes(text.ToString()));
                }
                else
                {
                    using (var localBuilder = ReusableLayoutBuilder.Allocate())
                    {
                        Layout.RenderAppendBuilder(logEvent, localBuilder.Result, false);
                        if (NewLine)
                        {
                            localBuilder.Result.Append(LineEnding.NewLineCharacters);
                        }

                        InternalLogger.Trace("{0} - Sending {1} chars", this, localBuilder.Result.Length);

                        using (var localBuffer = _reusableEncodingBuffer.Allocate())
                        {
#if !SILVERLIGHT
                            if (localBuilder.Result.Length <= localBuffer.Result.Length)
                            {
                                localBuilder.Result.CopyTo(0, localBuffer.Result, 0, localBuilder.Result.Length);
                                return(Encoding.GetBytes(localBuffer.Result, 0, localBuilder.Result.Length));
                            }
                            else
#endif
                            {
                                var rendered = localBuilder.Result.ToString();
                                return(Encoding.GetBytes(rendered));
                            }
                        }
                    }
                }
            }
            else
            {
                var rendered = Layout.Render(logEvent);
                InternalLogger.Trace("{0} - Sending: {1}", this, rendered);
                if (NewLine)
                {
                    rendered += LineEnding.NewLineCharacters;
                }
                return(Encoding.GetBytes(rendered));
            }
        }
Exemplo n.º 13
0
            /// <summary>
            /// Renders the layout for the specified logging event by invoking layout renderers.
            /// </summary>
            /// <param name="logEvent">The logging event.</param>
            /// <returns>The rendered layout.</returns>
            protected override string GetFormattedMessage(LogEventInfo logEvent)
            {
                string cached;

                if (logEvent.TryGetCachedLayoutValue(this, out cached))
                {
                    return(cached);
                }

                var sb = new StringBuilder();

                this.parent.RenderHeader(sb);
                return(logEvent.AddCachedLayoutValue(this, sb.ToString()));
            }
Exemplo n.º 14
0
        /// <summary>
        /// Formats the log event for write.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            var sb = new StringBuilder();

            RenderAllColumns(logEvent, sb);
            return(logEvent.AddCachedLayoutValue(this, sb.ToString()));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Render the raw filename from Layout
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        /// <param name="reusableBuilder">StringBuilder to minimize allocations [optional].</param>
        /// <returns>String representation of a layout.</returns>
        private string GetRenderedFileName(LogEventInfo logEvent, System.Text.StringBuilder reusableBuilder = null)
        {
            if (_cleanedFixedResult != null)
            {
                return(_cleanedFixedResult);
            }

            if (_layout == null)
            {
                return(null);
            }

            if (reusableBuilder != null)
            {
                if (!_layout.ThreadAgnostic || _layout.MutableUnsafe)
                {
                    object cachedResult;
                    if (logEvent.TryGetCachedLayoutValue(_layout, out cachedResult))
                    {
                        return(cachedResult?.ToString() ?? string.Empty);
                    }
                }

                _layout.RenderAppendBuilder(logEvent, reusableBuilder);

                if (_cachedPrevRawFileName != null)
                {
                    // If old filename matches the newly rendered, then no need to call StringBuilder.ToString()
                    if (reusableBuilder.EqualTo(_cachedPrevRawFileName))
                    {
                        return(_cachedPrevRawFileName);
                    }
                }

                _cachedPrevRawFileName   = reusableBuilder.ToString();
                _cachedPrevCleanFileName = null;
                return(_cachedPrevRawFileName);
            }
            else
            {
                return(_layout.Render(logEvent));
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Gets the bytes to be written.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        /// <returns>Byte array.</returns>
        protected virtual byte[] GetBytesToWrite(LogEventInfo logEvent)
        {
            using (var localBuffer = _reusableEncodingBuffer.Allocate())
            {
                if (!NewLine && logEvent.TryGetCachedLayoutValue(Layout, out var text))
                {
                    return(GetBytesFromString(localBuffer.Result, text?.ToString() ?? string.Empty));
                }
                else
                {
                    using (var localBuilder = ReusableLayoutBuilder.Allocate())
                    {
                        Layout.RenderAppendBuilder(logEvent, localBuilder.Result, false);
                        if (NewLine)
                        {
                            localBuilder.Result.Append(LineEnding.NewLineCharacters);
                        }

                        return(GetBytesFromStringBuilder(localBuffer.Result, localBuilder.Result));
                    }
                }
            }
        }
        /// <summary>
        /// Gets the bytes to be written.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        /// <returns>Byte array.</returns>
        protected virtual byte[] GetBytesToWrite(LogEventInfo logEvent)
        {
            if (OptimizeBufferReuse)
            {
                using (var localBuffer = _reusableEncodingBuffer.Allocate())
                {
                    if (!NewLine && logEvent.TryGetCachedLayoutValue(Layout, out var text))
                    {
                        return(GetBytesFromString(localBuffer.Result, text?.ToString() ?? string.Empty));
                    }
                    else
                    {
                        using (var localBuilder = ReusableLayoutBuilder.Allocate())
                        {
                            Layout.RenderAppendBuilder(logEvent, localBuilder.Result, false);
                            if (NewLine)
                            {
                                localBuilder.Result.Append(LineEnding.NewLineCharacters);
                            }

                            return(GetBytesFromStringBuilder(localBuffer.Result, localBuilder.Result));
                        }
                    }
                }
            }
            else
            {
                var rendered = Layout.Render(logEvent);
                InternalLogger.Trace("NetworkTarget(Name={0}): Sending: {1}", Name, rendered);
                if (NewLine)
                {
                    rendered += LineEnding.NewLineCharacters;
                }
                return(Encoding.GetBytes(rendered));
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if(Renderers == null)
                throw new InvalidOperationException("required run CreateParameters method");

            if (_fixedText != null)
                return _fixedText;

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
                return cachedValue;

            int initialSize = _maxRenderedLength;
            if (initialSize > MaxInitialRenderBufferLength)
                initialSize = MaxInitialRenderBufferLength;

            var builder = new StringBuilder(initialSize);

            foreach (LayoutRenderer renderer in Renderers)
            {
                try
                {
                    renderer.Render(builder, logEvent);
                }
                catch (Exception exception)
                {
                    if (exception.MustBeRethrown())
                        throw;

                    if (InternalLogger.IsWarnEnabled)
                        InternalLogger.Warn("Exception in {0}.Append(): {1}.", renderer.GetType().FullName, exception);
                }
            }

            if (builder.Length > _maxRenderedLength)
                _maxRenderedLength = builder.Length;

            string value = builder.ToString();
            logEvent.AddCachedLayoutValue(this, value);
            return value;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Formats the log event for write.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return cachedValue;
            }

            var sb = new StringBuilder();

            //Memory profiling pointed out that using a foreach-loop was allocating
            //an Enumerator. Switching to a for-loop avoids the memory allocation.
            for (int i = 0; i < this.Columns.Count; i++)
            {
                CsvColumn col = this.Columns[i];
                if (i != 0)
                {
                    sb.Append(this.actualColumnDelimiter);
                }

                bool useQuoting;
                string text = col.Layout.Render(logEvent);

                switch (this.Quoting)
                {
                    case CsvQuotingMode.Nothing:
                        useQuoting = false;
                        break;

                    case CsvQuotingMode.All:
                        useQuoting = true;
                        break;

                    default:
                    case CsvQuotingMode.Auto:
                        if (text.IndexOfAny(this.quotableCharacters) >= 0)
                        {
                            useQuoting = true;
                        }
                        else
                        {
                            useQuoting = false;
                        }

                        break;
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }

                if (useQuoting)
                {
                    sb.Append(text.Replace(this.QuoteChar, this.doubleQuoteChar));
                }
                else
                {
                    sb.Append(text);
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }
            }

            return logEvent.AddCachedLayoutValue(this, sb.ToString());
        }
Exemplo n.º 20
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (this.fixedText != null)
            {
                return this.fixedText;
            }

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return cachedValue;
            }

            int initialSize = this.maxRenderedLength;
            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }

            var builder = new StringBuilder(initialSize);

            foreach (LayoutRenderer renderer in this.Renderers)
            {
                try
                {
                    renderer.Render(builder, logEvent);
                }
                catch (Exception exception)
                {
                    if (exception.MustBeRethrown())
                    {
                        throw;
                    }

                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.Append(): {1}.", renderer.GetType().FullName, exception);
                    }
                }
            }

            if (builder.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = builder.Length;
            }

            string value = builder.ToString();
            logEvent.AddCachedLayoutValue(this, value);
            return value;
        }
Exemplo n.º 21
0
        /// <summary>
        ///     Formats the log event for write.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            var sb    = new StringBuilder();
            var first = true;

            foreach (var col in Columns)
            {
                if (!first)
                {
                    sb.Append(actualColumnDelimiter);
                }

                first = false;

                bool useQuoting;
                var  text = col.Layout.Render(logEvent);

                switch (Quoting)
                {
                case CsvQuotingMode.Nothing:
                    useQuoting = false;
                    break;

                case CsvQuotingMode.All:
                    useQuoting = true;
                    break;

                default:
                case CsvQuotingMode.Auto:
                    if (text.IndexOfAny(quotableCharacters) >= 0)
                    {
                        useQuoting = true;
                    }
                    else
                    {
                        useQuoting = false;
                    }

                    break;
                }

                if (useQuoting)
                {
                    sb.Append(QuoteChar);
                }

                if (useQuoting)
                {
                    sb.Append(text.Replace(QuoteChar, doubleQuoteChar));
                }
                else
                {
                    sb.Append(text);
                }

                if (useQuoting)
                {
                    sb.Append(QuoteChar);
                }
            }

            return(logEvent.AddCachedLayoutValue(this, sb.ToString()));
        }
Exemplo n.º 22
0
        /// <summary>
        /// Renders the layout for the specified logging event by invoking layout renderers
        /// that make up the event.
        /// </summary>
        /// <param name="logEvent">The logging event.</param>
        /// <returns>The rendered layout.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (IsFixedText)
            {
                return this.fixedText;
            }

            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return cachedValue;
            }

            int initialSize = this.maxRenderedLength;
            if (initialSize > MaxInitialRenderBufferLength)
            {
                initialSize = MaxInitialRenderBufferLength;
            }

            var builder = new StringBuilder(initialSize);

            //Memory profiling pointed out that using a foreach-loop was allocating
            //an Enumerator. Switching to a for-loop avoids the memory allocation.
            for (int i = 0; i < this.Renderers.Count; i++)
            {
                LayoutRenderer renderer = this.Renderers[i];
                try
                {
                    renderer.Render(builder, logEvent);
                }
                catch (Exception exception)
                {
                    //also check IsErrorEnabled, otherwise 'MustBeRethrown' writes it to Error

                    //check for performance
                    if (InternalLogger.IsWarnEnabled || InternalLogger.IsErrorEnabled)
                    {
                        InternalLogger.Warn(exception, "Exception in '{0}.Append()'", renderer.GetType().FullName);
                    }

                    if (exception.MustBeRethrown())
                    {
                        throw;
                    }
                }
            }

            if (builder.Length > this.maxRenderedLength)
            {
                this.maxRenderedLength = builder.Length;
            }

            string value = builder.ToString();
            logEvent.AddCachedLayoutValue(this, value);
            return value;
        }
Exemplo n.º 23
0
        /// <summary>
        /// Formats the log event for write.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return(cachedValue);
            }

            var sb = new StringBuilder();

            //Memory profiling pointed out that using a foreach-loop was allocating
            //an Enumerator. Switching to a for-loop avoids the memory allocation.
            for (int i = 0; i < this.Columns.Count; i++)
            {
                CsvColumn col = this.Columns[i];
                if (i != 0)
                {
                    sb.Append(this.actualColumnDelimiter);
                }

                bool   useQuoting;
                string text = col.Layout.Render(logEvent);

                switch (this.Quoting)
                {
                case CsvQuotingMode.Nothing:
                    useQuoting = false;
                    break;

                case CsvQuotingMode.All:
                    useQuoting = true;
                    break;

                default:
                case CsvQuotingMode.Auto:
                    if (text.IndexOfAny(this.quotableCharacters) >= 0)
                    {
                        useQuoting = true;
                    }
                    else
                    {
                        useQuoting = false;
                    }

                    break;
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }

                if (useQuoting)
                {
                    sb.Append(text.Replace(this.QuoteChar, this.doubleQuoteChar));
                }
                else
                {
                    sb.Append(text);
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }
            }

            return(logEvent.AddCachedLayoutValue(this, sb.ToString()));
        }
Exemplo n.º 24
0
            /// <summary>
            /// Renders the layout for the specified logging event by invoking layout renderers.
            /// </summary>
            /// <param name="logEvent">The logging event.</param>
            /// <returns>The rendered layout.</returns>
            protected override string GetFormattedMessage(LogEventInfo logEvent)
            {
                string cached;

                if (logEvent.TryGetCachedLayoutValue(this, out cached))
                {
                    return cached;
                }

                return logEvent.AddCachedLayoutValue(this, this.parent.GetHeader());
            }
Exemplo n.º 25
0
        /// <summary>
        /// Formats the log event for write.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            string cachedValue;

            if (logEvent.TryGetCachedLayoutValue(this, out cachedValue))
            {
                return cachedValue;
            }

            var sb = new StringBuilder();
            bool first = true;

            foreach (CsvColumn col in this.Columns)
            {
                if (!first)
                {
                    sb.Append(this.actualColumnDelimiter);
                }

                first = false;

                bool useQuoting;
                string text = col.Layout.Render(logEvent);

                switch (this.Quoting)
                {
                    case CsvQuotingMode.Nothing:
                        useQuoting = false;
                        break;

                    case CsvQuotingMode.All:
                        useQuoting = true;
                        break;

                    default:
                    case CsvQuotingMode.Auto:
                        if (text.IndexOfAny(this.quotableCharacters) >= 0)
                        {
                            useQuoting = true;
                        }
                        else
                        {
                            useQuoting = false;
                        }

                        break;
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }

                if (useQuoting)
                {
                    sb.Append(text.Replace(this.QuoteChar, this.doubleQuoteChar));
                }
                else
                {
                    sb.Append(text);
                }

                if (useQuoting)
                {
                    sb.Append(this.QuoteChar);
                }
            }

            return logEvent.AddCachedLayoutValue(this, sb.ToString());
        }