public void TryWriteProperty_should_not_write_if_event_has_no_properties()
        {
            var writer = new StringWriter();

            FragmentHelpers.TryWriteProperty(null, null, writer);
            writer.ToString().Should().BeEmpty();
        }
        public void TryWriteProperty_should_write_property_without_format()
        {
            const string propName = "name";
            const int    value    = 123;

            var writer = new StringWriter();
            var le     = LogEvent().WithProperty(propName, value);
            var prop   = FragmentHelpers.GetPropertyOrNull(le, propName);

            FragmentHelpers.TryWriteProperty(prop, null, writer);
            writer.ToString().Should().Be(value.ToString());
        }
        public void TryWriteProperty_should_write_property_with_format()
        {
            const string propName = "name";
            const string format   = "yyyy-MM-dd";
            var          value    = DateTime.Now;

            var writer = new StringWriter();
            var le     = LogEvent().WithProperty(propName, value);
            var prop   = FragmentHelpers.GetPropertyOrNull(le, propName);

            FragmentHelpers.TryWriteProperty(prop, format, writer);
            writer.ToString().Should().Be(value.ToString(format));
        }
        private static void WriteProperties(IReadOnlyDictionary <string, object> properties, TextWriter writer)
        {
            if (properties == null || properties.Count == 0)
            {
                return;
            }

            writer.Write("[properties: ");

            var i = 0;

            foreach (var pair in properties)
            {
                writer.Write(pair.Key);
                writer.Write(" = ");
                FragmentHelpers.TryWriteProperty(pair.Value, null, writer);
                if (++i < properties.Count)
                {
                    writer.Write(", ");
                }
            }

            writer.Write("]");
        }
 public void Render(LogEvent @event, TextWriter writer) =>
 FragmentHelpers.TryWriteProperty(FragmentHelpers.GetPropertyOrNull(@event, property), format, writer);