Example #1
0
        private bool RenderAppendJsonPropertyValue(JsonAttribute attrib, LogEventInfo logEvent, bool renderEmptyValue, StringBuilder sb, bool beginJsonMessage)
        {
            BeginJsonProperty(sb, attrib.Name, beginJsonMessage);
            if (attrib.Encode)
            {
                // "\"{0}\":{1}\"{2}\""
                sb.Append('"');
            }
            int beforeValueLength = sb.Length;

            attrib.LayoutWrapper.RenderAppendBuilder(logEvent, sb);
            if (!renderEmptyValue && beforeValueLength == sb.Length)
            {
                return(false);
            }
            if (attrib.Encode)
            {
                sb.Append('"');
            }
            return(true);
        }
Example #2
0
        private void AppendJsonAttributeValue(JsonAttribute attrib, string text, StringBuilder sb, bool first)
        {
            if (!first)
            {
                sb.EnsureCapacity(sb.Length + attrib.Name.Length + text.Length + 12);
                sb.Append(',');
                if (!this.SuppressSpaces)
                {
                    sb.Append(' ');
                }
            }

            sb.Append('"');
            sb.Append(attrib.Name);
            sb.Append('"');
            sb.Append(':');
            if (!this.SuppressSpaces)
            {
                sb.Append(' ');
            }

            if (attrib.Encode)
            {
                // "\"{0}\":{1}\"{2}\""
                sb.Append('"');
                sb.Append(text);
                sb.Append('"');
            }
            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).
                // "\"{0}\":{1}{2}";
                sb.Append(text);
            }
        }
Example #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)
        {
            StringBuilder sb = null;

            //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 attrib = this.Attributes[i];
                string text = attrib.LayoutWrapper.Render(logEvent);
                if (!string.IsNullOrEmpty(text))
                {
                    bool first = sb == null;
                    if (first)
                    {
                        sb = new StringBuilder(attrib.Name.Length + text.Length + 10);
                        sb.Append(SuppressSpaces ? "{" : "{ ");
                    }
                    AppendJsonAttributeValue(attrib, text, sb, first);
                }
            }

            if (this.IncludeAllProperties && logEvent.HasProperties)
            {
                JsonAttribute dynAttrib = null;
                foreach (var prop in logEvent.Properties)
                {
                    //Determine property name
                    string propName = prop.Key.ToString();

                    //Skips properties in the ExcludeProperties list
                    if (this.ExcludeProperties.Contains(propName)) continue;

                    if (dynAttrib == null)
                        dynAttrib = new JsonAttribute();

                    if (prop.Value == null)
                    {
                        dynAttrib.Name = propName;
                        dynAttrib.Encode = false;    // Don't put quotes around null values;
                        dynAttrib.Layout = "null";
                    }
                    else
                    {
                        System.Type objType = prop.Value.GetType();
                        System.TypeCode objTypeCode = System.Type.GetTypeCode(objType);
                        if (objTypeCode == System.TypeCode.Boolean || IsNumeric(objType, objTypeCode))
                        {
                            dynAttrib.Name = propName;
                            dynAttrib.Encode = false;    //Don't put quotes around numbers or boolean values
                            dynAttrib.Layout = string.Concat("${event-properties:item=", propName, "}");
                        }
                        else
                        {
                            dynAttrib.Name = propName;
                            dynAttrib.Encode = true;
                            dynAttrib.Layout = string.Concat("${event-properties:item=", propName, "}");
                        }
                    }

                    string text = dynAttrib.LayoutWrapper.Render(logEvent);
                    if (!string.IsNullOrEmpty(text))
                    {
                        bool first = sb == null;
                        if (first)
                        {
                            sb = new StringBuilder(dynAttrib.Name.Length + text.Length + 10);
                            sb.Append(SuppressSpaces ? "{" : "{ ");
                        }
                        AppendJsonAttributeValue(dynAttrib, text, sb, first);
                    }
                }
            }

            if (sb == null)
            {
                if (!RenderEmptyObject)
                    return string.Empty;
                else
                    return SuppressSpaces ? "{}" : "{  }";
            }
            sb.Append(SuppressSpaces ? "}" : " }");
            return sb.ToString();
        }
Example #4
0
        private void AppendJsonAttributeValue(JsonAttribute attrib, string text, StringBuilder sb, bool first)
        {
            if (!first)
            {
                sb.EnsureCapacity(sb.Length + attrib.Name.Length + text.Length + 12);
                sb.Append(',');
                if (!this.SuppressSpaces)
                    sb.Append(' ');
            }

            sb.Append('"');
            sb.Append(attrib.Name);
            sb.Append('"');
            sb.Append(':');
            if (!this.SuppressSpaces)
                sb.Append(' ');

            if (attrib.Encode)
            {
                // "\"{0}\":{1}\"{2}\""
                sb.Append('"');
                sb.Append(text);
                sb.Append('"');
            }
            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).
                // "\"{0}\":{1}{2}";
                sb.Append(text);
            }
        }
Example #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)
        {
            StringBuilder sb = null;

            //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    attrib = this.Attributes[i];
                string text   = attrib.LayoutWrapper.Render(logEvent);
                if (!string.IsNullOrEmpty(text))
                {
                    bool first = sb == null;
                    if (first)
                    {
                        sb = new StringBuilder(attrib.Name.Length + text.Length + 10);
                        sb.Append(SuppressSpaces ? "{" : "{ ");
                    }
                    AppendJsonAttributeValue(attrib, text, sb, first);
                }
            }

            if (this.IncludeAllProperties && logEvent.HasProperties)
            {
                JsonAttribute dynAttrib = null;
                foreach (var prop in logEvent.Properties)
                {
                    //Determine property name
                    string propName = prop.Key.ToString();

                    //Skips properties in the ExcludeProperties list
                    if (this.ExcludeProperties.Contains(propName))
                    {
                        continue;
                    }

                    if (dynAttrib == null)
                    {
                        dynAttrib = new JsonAttribute();
                    }

                    if (prop.Value == null)
                    {
                        dynAttrib.Name   = propName;
                        dynAttrib.Encode = false;    // Don't put quotes around null values;
                        dynAttrib.Layout = "null";
                    }
                    else
                    {
                        System.Type     objType     = prop.Value.GetType();
                        System.TypeCode objTypeCode = System.Type.GetTypeCode(objType);
                        if (objTypeCode == System.TypeCode.Boolean || IsNumeric(objType, objTypeCode))
                        {
                            dynAttrib.Name   = propName;
                            dynAttrib.Encode = false;    //Don't put quotes around numbers or boolean values
                            dynAttrib.Layout = string.Concat("${event-properties:item=", propName, "}");
                        }
                        else
                        {
                            dynAttrib.Name   = propName;
                            dynAttrib.Encode = true;
                            dynAttrib.Layout = string.Concat("${event-properties:item=", propName, "}");
                        }
                    }

                    string text = dynAttrib.LayoutWrapper.Render(logEvent);
                    if (!string.IsNullOrEmpty(text))
                    {
                        bool first = sb == null;
                        if (first)
                        {
                            sb = new StringBuilder(dynAttrib.Name.Length + text.Length + 10);
                            sb.Append(SuppressSpaces ? "{" : "{ ");
                        }
                        AppendJsonAttributeValue(dynAttrib, text, sb, first);
                    }
                }
            }

            if (sb == null)
            {
                if (!RenderEmptyObject)
                {
                    return(string.Empty);
                }
                else
                {
                    return(SuppressSpaces ? "{}" : "{  }");
                }
            }
            sb.Append(SuppressSpaces ? "}" : " }");
            return(sb.ToString());
        }