src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Output/XmlPropertyFormatter.cs
/// <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>
Simplify the object so as to make handling the serialized
representation easier.
Simplify the object so as to make handling the serialized representation easier .
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;
        }
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 , " " , options . DictionaryElementName ) ; } } var key = SimplifyScalar ( element . Key . Value ) ; if ( options . UsePropertyKeyAsElementName ) { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , GetValidElementName ( key ) , itemValue ) ; } else { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , options . ItemElementName , key , itemValue ) ; } } if ( ! isEmpty && ! options . OmitDictionaryContainerElement ) { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , 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 , " " , options . SequenceElementName ) ; } } sb . AppendFormat ( CultureInfo . InvariantCulture , " " , options . ItemElementName , itemValue ) ; } if ( ! isEmpty && ! options . OmitSequenceContainerElement ) { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , 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 , " " , GetValidElementName ( str . TypeTag ) ) ; } else { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , options . StructureElementName , str . TypeTag ) ; } } } if ( options . UsePropertyKeyAsElementName ) { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , GetValidElementName ( element . Key ) , itemValue ) ; } else { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , options . PropertyElementName , element . Key , itemValue ) ; } } if ( ! isEmpty && ! options . OmitStructureContainerElement ) { if ( options . UsePropertyKeyAsElementName ) { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , GetValidElementName ( str . TypeTag ) ) ; } else { sb . AppendFormat ( CultureInfo . InvariantCulture , " " , options . StructureElementName ) ; } } return sb . ToString ( ) ; } return null ; }
Simplify the object so as to make handling the serialized
representation easier.
Simplify the object so as to make handling the serialized representation easier .
A simplified representation.
A simplified representation .
value
LogEventPropertyValue
The value to simplify (possibly null).
The value to simplify ( possibly null ) .
options
ColumnOptions.PropertiesColumnOptions
Options to use during formatting
Options to use during formatting