private static void AddRenderedValue(
            LogEventInfo source, IDictionary <string, object> dest,
            JsonEncodeLayoutRendererWrapper renderer, JsonAttribute attribute)
        {
            renderer.Inner      = attribute.Layout;
            renderer.JsonEncode = attribute.Encode;

            string renderedValue;

            try
            {
                renderedValue = renderer.Render(source);
            }
            catch (Exception ex)
            {
                renderedValue = $"Render failed: {ex.GetType().Name} {ex.Message}";
            }

            if (string.IsNullOrEmpty(renderedValue))
            {
                return;
            }

            Mapper.HarvestStringToDictionary(dest,
                                             attribute.Name, renderedValue, "attributes_");
        }
Esempio n. 2
0
        /// <summary>
        /// Formats the log event as a JSON document for writing.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A JSON string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            var jsonWrapper = new JsonEncodeLayoutRendererWrapper();
            var sb          = new StringBuilder();

            sb.Append("{ ");
            bool first = true;

            foreach (var col in this.Attributes)
            {
                jsonWrapper.Inner = col.Layout;
                string text = jsonWrapper.Render(logEvent);

                if (!string.IsNullOrEmpty(text))
                {
                    if (!first)
                    {
                        sb.Append(", ");
                    }

                    first = false;

                    sb.AppendFormat("\"{0}\": \"{1}\"", col.Name, text);
                }
            }

            sb.Append(" }");

            return(sb.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Formats the log event as a JSON document for writing.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A JSON string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            var jsonWrapper = new JsonEncodeLayoutRendererWrapper();
            var sb = new StringBuilder();
            sb.Append("{ ");
            bool first = true;

            foreach (var col in this.Attributes)
            {
                jsonWrapper.Inner = col.Layout;
                string text = jsonWrapper.Render(logEvent);
                
                if (!string.IsNullOrEmpty(text))
                {
                    if (!first) 
                    {
                        sb.Append(", ");
                    }

                    first = false;

                    sb.AppendFormat("\"{0}\": \"{1}\"", col.Name, text);
                }
            }

            sb.Append(" }");

            return sb.ToString();
        }
Esempio n. 4
0
        /// <summary>
        /// Formats the log event as a JSON document for writing.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A JSON string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            var  jsonWrapper = new JsonEncodeLayoutRendererWrapper();
            var  sb          = new StringBuilder();
            bool first       = true;
            bool hasContent  = false;

            //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.Attributes.Count; i++)
            {
                var col = this.Attributes[i];
                jsonWrapper.Inner      = col.Layout;
                jsonWrapper.JsonEncode = col.Encode;
                string text = jsonWrapper.Render(logEvent);

                if (!string.IsNullOrEmpty(text))
                {
                    if (!first)
                    {
                        sb.Append(",");
                        AppendIf(!this.SuppressSpaces, sb, " ");
                    }

                    first = false;

                    string format;

                    if (col.Encode)
                    {
                        format = "\"{0}\":{1}\"{2}\"";
                    }
                    else
                    {
                        //If encoding is disabled for current attribute, do not escape the value of the attribute.
                        //This enables user to write arbitrary string value (including JSON).
                        format = "\"{0}\":{1}{2}";
                    }

                    sb.AppendFormat(format, col.Name, !this.SuppressSpaces ? " " : "", text);
                    hasContent = true;
                }
            }

            var result = sb.ToString();

            if (!hasContent && !RenderEmptyObject)
            {
                return(string.Empty);
            }

            if (SuppressSpaces)
            {
                return("{" + result + "}");
            }
            return("{ " + result + " }");
        }
Esempio n. 5
0
        /// <summary>
        /// Formats the log event as a JSON document for writing.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A JSON string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            var jsonWrapper = new JsonEncodeLayoutRendererWrapper();
            var sb          = new StringBuilder();

            sb.Append("{");
            AppendIf(!this.SuppressSpaces, sb, " ");
            bool first = true;

            foreach (var col in this.Attributes)
            {
                jsonWrapper.Inner      = col.Layout;
                jsonWrapper.JsonEncode = col.Encode;
                string text = jsonWrapper.Render(logEvent);

                if (!string.IsNullOrEmpty(text))
                {
                    if (!first)
                    {
                        sb.Append(",");
                        AppendIf(!this.SuppressSpaces, sb, " ");
                    }

                    first = false;

                    string format;

                    if (col.Encode)
                    {
                        format = "\"{0}\":{1}\"{2}\"";
                    }
                    else
                    {
                        //If encoding is disabled for current attribute, do not escape the value of the attribute.
                        //This enables user to write arbitrary string value (including JSON).
                        format = "\"{0}\":{1}{2}";
                    }

                    sb.AppendFormat(format, col.Name, !this.SuppressSpaces ? " " : "", text);
                }
            }

            AppendIf(!this.SuppressSpaces, sb, " ");
            sb.Append("}");

            return(sb.ToString());
        }
Esempio n. 6
0
        /// <summary>
        /// Formats the log event as a JSON document for writing.
        /// </summary>
        /// <param name="logEvent">The log event to be formatted.</param>
        /// <returns>A JSON string representation of the log event.</returns>
        protected override string GetFormattedMessage(LogEventInfo logEvent)
        {
            var jsonWrapper = new JsonEncodeLayoutRendererWrapper();
            var sb = new StringBuilder();
            sb.Append("{ ");
            bool first = true;

            foreach (var col in this.Attributes)
            {
                jsonWrapper.Inner = col.Layout;
                jsonWrapper.JsonEncode = col.Encode;
                string text = jsonWrapper.Render(logEvent);

                if (!string.IsNullOrEmpty(text))
                {
                    if (!first)
                    {
                        sb.Append(", ");
                    }

                    first = false;

                    string format;

                    if(col.Encode)
                    {
                        format = "\"{0}\": \"{1}\"";
                    }
                    else
                    {
                        //If encoding is disabled for current attribute, do not escape the value of the attribute.
                        //This enables user to write arbitrary string value (including JSON).
                        format = "\"{0}\": {1}";
                    }

                    sb.AppendFormat(format, col.Name, text);
                }
            }

            sb.Append(" }");

            return sb.ToString();
        }
        private void AppendDataFromAttributes(LogEventInfo logEvent, IDictionary <string, object> result)
        {
            var layoutRendererWrapper = new JsonEncodeLayoutRendererWrapper();

            foreach (var jsonAttribute in Attributes)
            {
                layoutRendererWrapper.Inner      = jsonAttribute.Layout;
                layoutRendererWrapper.JsonEncode = jsonAttribute.Encode;
                var str = layoutRendererWrapper.Render(logEvent);
                if (string.IsNullOrEmpty(str))
                {
                    continue;
                }
                if (!result.ContainsKey(jsonAttribute.Name))
                {
                    result.Add(jsonAttribute.Name, str);
                }
            }
        }