AddCachedLayoutValue() private method

private AddCachedLayoutValue ( Layout layout, string value ) : string
layout Layout
value string
return string
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>
        public string GetFormattedMessage(LogEventInfo logEvent)
        {
            if (_fixedText != null)
                return _fixedText;

            string cachedValue = logEvent.GetCachedLayoutValue(this);
            if (cachedValue != null)
                return cachedValue;

            int size = 0;

            for (int i = 0; i < _renderers.Length; ++i)
            {
                LayoutRenderer app = _renderers[i];
                try
                {
                    int ebs = app.GetEstimatedBufferSize(logEvent);
                    size += ebs;
                }
                catch (Exception ex)
                {
                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.GetEstimatedBufferSize(): {1}.", app.GetType().FullName, ex);
                    }
                }
            }
            StringBuilder builder = new StringBuilder(size);

            for (int i = 0; i < _renderers.Length; ++i)
            {
                LayoutRenderer app = _renderers[i];
                try
                {
                    app.Append(builder, logEvent);
                }
                catch (Exception ex)
                {
                    if (InternalLogger.IsWarnEnabled)
                    {
                        InternalLogger.Warn("Exception in {0}.Append(): {1}.", app.GetType().FullName, ex);
                    }
                }
            }

            string value = builder.ToString();
            logEvent.AddCachedLayoutValue(this, value);
            return value;
        }