Ejemplo n.º 1
0
        /// <summary>
        /// <inheritdoc cref="ObjectValueFormatter.Format(TextWriter,object,string,IFormatProvider)"/>
        /// </summary>
        public static void Format(
            [NotNull] TextWriter writer,
            [CanBeNull] object value,
            [CanBeNull] string format = null,
            [CanBeNull] IFormatProvider formatProvider = null)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (value == null)
            {
                return;
            }

            if (PaddingFormatHelper.TryParseFormat(format, out var insertLeadingSpace, out var insertTrailingSpace))
            {
                format = null;
            }

            if (insertLeadingSpace)
            {
                writer.WriteSpace();
            }

            ObjectValueFormatter.Format(writer, value, format, formatProvider);

            if (insertTrailingSpace)
            {
                writer.WriteSpace();
            }
        }
Ejemplo n.º 2
0
        public void TryParseFormat_should_correctly_parse_valid_formats(
            string format,
            bool leadingSpaceExpected,
            bool trailingSpaceExpected)
        {
            PaddingFormatHelper.TryParseFormat(format, out var insertLeadingSpace, out var insertTrailingSpace).Should().BeTrue();

            insertLeadingSpace.Should().Be(leadingSpaceExpected);

            trailingSpaceExpected.Should().Be(insertTrailingSpace);
        }
        public static void Format(
            [NotNull] LogEvent @event,
            [NotNull] TextWriter writer,
            [CanBeNull] object value,
            [CanBeNull] string format = null,
            [CanBeNull] IFormatProvider formatProvider = null)
        {
            if (@event == null)
            {
                throw new ArgumentNullException(nameof(@event));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (value is OperationContextValue contextValue)
            {
                value = contextValue.ToString();
            }

            if (value is not string template || !TemplateTokenizer.CanContainNamedTokens(template))
            {
                PropertyValueFormatter.Format(writer, value, format, formatProvider);
                return;
            }

            var tokens = TemplatesCache.ObtainTokens(template);

            PaddingFormatHelper.TryParseFormat(format, out var insertLeadingSpace, out var insertTrailingSpace);

            if (insertLeadingSpace)
            {
                writer.WriteSpace();
            }

            foreach (var token in tokens)
            {
                token.Render(@event, writer, formatProvider);
            }

            if (insertTrailingSpace)
            {
                writer.WriteSpace();
            }
        }
Ejemplo n.º 4
0
 public void TryParseFormat_should_return_false_for_invalid_formats(string format)
 {
     PaddingFormatHelper.TryParseFormat(format, out _, out _).Should().BeFalse();
 }