Serilog.Events.DictionaryValue.Render C# (CSharp) Method

Render() public method

Render the value to the output.
public Render ( TextWriter output, string format = null, IFormatProvider formatProvider = null ) : void
output System.IO.TextWriter The output.
format string A format string applied to the value, or null.
formatProvider IFormatProvider A format provider to apply to the value, or null to use the default.
return void
        public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
        {
            if (output == null) throw new ArgumentNullException(nameof(output));

            output.Write('[');
            var delim = "(";
            foreach (var kvp in Elements)
            {
                output.Write(delim);
                delim = ", (";
                kvp.Key.Render(output, null, formatProvider);
                output.Write(": ");
                kvp.Value.Render(output, null, formatProvider);
                output.Write(")");
            }

            output.Write(']');
        }

Usage Example

Exemplo n.º 1
0
        public void ADictionaryValueRendersAsMappingOfKeysToValues()
        {
            var dict = new DictionaryValue(new[] {
                new KeyValuePair<ScalarValue, LogEventPropertyValue>(
                    new ScalarValue(1), new ScalarValue("hello")),
                new KeyValuePair<ScalarValue, LogEventPropertyValue>(
                    new ScalarValue("world"), new SequenceValue(new [] { new ScalarValue(1.2)  }))
            });

            var sw = new StringWriter();
            dict.Render(sw);

            var rendered = sw.ToString();

            Assert.AreEqual("[(1: \"hello\"), (\"world\": [1.2])]", rendered);
        }