示例#1
0
        private string SimplifySequence(ColumnOptions.PropertiesColumnOptions options, SequenceValue seq)
        {
            var sb = new StringBuilder();

            var isEmpty = true;

            foreach (var element in seq.Elements)
            {
                var itemValue = Simplify(element, options);
                if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                {
                    continue;
                }

                if (isEmpty)
                {
                    isEmpty = false;
                    if (!options.OmitSequenceContainerElement)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", options.SequenceElementName);
                    }
                }

                sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", options.ItemElementName, itemValue);
            }

            if (!isEmpty && !options.OmitSequenceContainerElement)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.SequenceElementName);
            }

            return(sb.ToString());
        }
示例#2
0
        private string SimplifyStructure(ColumnOptions.PropertiesColumnOptions options, StructureValue str)
        {
            var props = str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value, options));

            var sb = new StringBuilder();

            var isEmpty = true;

            foreach (var element in props)
            {
                var itemValue = element.Value;
                if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                {
                    continue;
                }

                if (isEmpty)
                {
                    isEmpty = false;
                    if (!options.OmitStructureContainerElement)
                    {
                        if (options.UsePropertyKeyAsElementName)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", GetValidElementName(str.TypeTag));
                        }
                        else
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} type='{1}'>", options.StructureElementName, str.TypeTag);
                        }
                    }
                }

                if (options.UsePropertyKeyAsElementName)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", GetValidElementName(element.Key), itemValue);
                }
                else
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} key='{1}'>{2}</{0}>", options.PropertyElementName,
                                    element.Key, itemValue);
                }
            }

            if (!isEmpty && !options.OmitStructureContainerElement)
            {
                if (options.UsePropertyKeyAsElementName)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", GetValidElementName(str.TypeTag));
                }
                else
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.StructureElementName);
                }
            }

            return(sb.ToString());
        }
示例#3
0
        private string SimplifyDictionary(ColumnOptions.PropertiesColumnOptions options, DictionaryValue dict)
        {
            var sb = new StringBuilder();

            var isEmpty = true;

            foreach (var element in dict.Elements)
            {
                var itemValue = Simplify(element.Value, options);
                if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                {
                    continue;
                }

                if (isEmpty)
                {
                    isEmpty = false;
                    if (!options.OmitDictionaryContainerElement)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", options.DictionaryElementName);
                    }
                }

                var key = SimplifyScalar(element.Key.Value);
                if (options.UsePropertyKeyAsElementName)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", GetValidElementName(key), itemValue);
                }
                else
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} key='{1}'>{2}</{0}>", options.ItemElementName, key, itemValue);
                }
            }

            if (!isEmpty && !options.OmitDictionaryContainerElement)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.DictionaryElementName);
            }

            return(sb.ToString());
        }
示例#4
0
        public string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options)
        {
            if (value is ScalarValue scalar)
            {
                return(SimplifyScalar(scalar.Value));
            }

            if (value is DictionaryValue dict)
            {
                return(SimplifyDictionary(options, dict));
            }

            if (value is SequenceValue seq)
            {
                return(SimplifySequence(options, seq));
            }

            if (value is StructureValue str)
            {
                return(SimplifyStructure(options, str));
            }

            return(null);
        }
        /// <summary>
        ///     Simplify the object so as to make handling the serialized
        ///     representation easier.
        /// </summary>
        /// <param name="value">The value to simplify (possibly null).</param>
        /// <param name="options">Options to use during formatting</param>
        /// <returns>A simplified representation.</returns>
        public static string Simplify(LogEventPropertyValue value, ColumnOptions.PropertiesColumnOptions options)
        {
            if (value is ScalarValue scalar)
            {
                return(SimplifyScalar(scalar.Value));
            }

            if (value is DictionaryValue dict)
            {
                var sb = new StringBuilder();

                var isEmpty = true;

                foreach (var element in dict.Elements)
                {
                    var itemValue = Simplify(element.Value, options);
                    if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                    {
                        continue;
                    }

                    if (isEmpty)
                    {
                        isEmpty = false;
                        if (!options.OmitDictionaryContainerElement)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", options.DictionaryElementName);
                        }
                    }

                    var key = SimplifyScalar(element.Key.Value);
                    if (options.UsePropertyKeyAsElementName)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", GetValidElementName(key), itemValue);
                    }
                    else
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} key='{1}'>{2}</{0}>", options.ItemElementName, key, itemValue);
                    }
                }

                if (!isEmpty && !options.OmitDictionaryContainerElement)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.DictionaryElementName);
                }

                return(sb.ToString());
            }

            if (value is SequenceValue seq)
            {
                var sb = new StringBuilder();

                var isEmpty = true;

                foreach (var element in seq.Elements)
                {
                    var itemValue = Simplify(element, options);
                    if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                    {
                        continue;
                    }

                    if (isEmpty)
                    {
                        isEmpty = false;
                        if (!options.OmitSequenceContainerElement)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", options.SequenceElementName);
                        }
                    }

                    sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", options.ItemElementName, itemValue);
                }

                if (!isEmpty && !options.OmitSequenceContainerElement)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.SequenceElementName);
                }

                return(sb.ToString());
            }

            if (value is StructureValue str)
            {
                var props = str.Properties.ToDictionary(p => p.Name, p => Simplify(p.Value, options));

                var sb = new StringBuilder();

                var isEmpty = true;

                foreach (var element in props)
                {
                    var itemValue = element.Value;
                    if (options.OmitElementIfEmpty && string.IsNullOrEmpty(itemValue))
                    {
                        continue;
                    }

                    if (isEmpty)
                    {
                        isEmpty = false;
                        if (!options.OmitStructureContainerElement)
                        {
                            if (options.UsePropertyKeyAsElementName)
                            {
                                sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>", GetValidElementName(str.TypeTag));
                            }
                            else
                            {
                                sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} type='{1}'>", options.StructureElementName, str.TypeTag);
                            }
                        }
                    }

                    if (options.UsePropertyKeyAsElementName)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0}>{1}</{0}>", GetValidElementName(element.Key), itemValue);
                    }
                    else
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "<{0} key='{1}'>{2}</{0}>", options.PropertyElementName,
                                        element.Key, itemValue);
                    }
                }

                if (!isEmpty && !options.OmitStructureContainerElement)
                {
                    if (options.UsePropertyKeyAsElementName)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", GetValidElementName(str.TypeTag));
                    }
                    else
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "</{0}>", options.StructureElementName);
                    }
                }

                return(sb.ToString());
            }

            return(null);
        }