Ejemplo n.º 1
0
        public void Acquire_should_hand_out_different_instances_when_called_multiple_times_in_a_row_from_same_thread()
        {
            var array1 = CharArrayCache.Acquire(10);
            var array2 = CharArrayCache.Acquire(10);
            var array3 = CharArrayCache.Acquire(10);

            array1.Should().NotBeSameAs(array2);
            array1.Should().NotBeSameAs(array3);
        }
Ejemplo n.º 2
0
        public void Acquire_should_cache_and_reuse_returned_instances()
        {
            var array = CharArrayCache.Acquire(10);

            for (var i = 0; i < 10; i++)
            {
                CharArrayCache.Return(array);

                CharArrayCache.Acquire(10).Should().BeSameAs(array);
            }
        }
        // TODO(krait): Can't it render messages directly to TextWriter? But let's not change it yet..
        /// <summary>
        /// <para>Renders the template to a fully formed log message, replacing placeholders with values from <paramref name="properties"/>.</para>
        /// <para>See <see cref="LogEvent.MessageTemplate"/> for details on <paramref name="template"/> format.</para>
        /// <para>This method never throws exceptions.</para>
        /// </summary>
        /// <param name="template">A message template with zero or more placeholders to substitute.</param>
        /// <param name="properties">A dictionary of properties to be used for substitution.</param>
        public static string FormatMessage([CanBeNull] string template, [CanBeNull] IReadOnlyDictionary <string, object> properties)
        {
            if (template == null)
            {
                return(null);
            }

            if (properties == null)
            {
                return(template);
            }

            var resultBuilder     = StringBuilderCache.Acquire(template.Length * 2);
            var tokenBuilderChars = CharArrayCache.Acquire(template.Length);
            var tokenBuilder      = new TokenBuilder(tokenBuilderChars);

            for (var i = 0; i < template.Length; i++)
            {
                var currentChar = template[i];

                if (currentChar != '{' && currentChar != '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                if (!tokenBuilder.IsEmpty)
                {
                    tokenBuilder.MoveToResult(resultBuilder);
                }

                if (i == template.Length - 1)
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var nextChar = template[i + 1];
                if (currentChar == nextChar)
                {
                    tokenBuilder.Add(currentChar);
                    i++;
                    continue;
                }

                if (currentChar == '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var findTokenResult = tokenBuilder.TryFindToken(template, i);

                i += tokenBuilder.Length - 1;

                if (findTokenResult)
                {
                    var key = tokenBuilder.GetKeyFromBuffer();
                    if (properties.TryGetValue(key, out var value))
                    {
                        resultBuilder.Append(FormatPropertyValue(value));
                        tokenBuilder.Clear();
                    }
                }
            }

            if (!tokenBuilder.IsEmpty)
            {
                tokenBuilder.MoveToResult(resultBuilder);
            }

            CharArrayCache.Return(tokenBuilderChars);

            return(StringBuilderCache.GetStringAndRelease(resultBuilder));
        }
Ejemplo n.º 4
0
 public void Acquire_should_always_provide_an_array_of_at_least_requested_capacity(int capacity)
 {
     CharArrayCache.Acquire(capacity).Length.Should().BeGreaterOrEqualTo(capacity);
 }