private static void AssertSerialize(
            string expectValue,
            string title           = null,
            string text            = null,
            string alertType       = null,
            string aggregationKey  = null,
            string sourceType      = null,
            int?dateHappened       = null,
            string priority        = null,
            string hostname        = null,
            string[] tags          = null,
            bool truncateIfTooLong = false)
        {
            var serializer = CreateSerializer();
            var statsEvent = new StatsEvent
            {
                Title             = title,
                Text              = text,
                AlertType         = alertType,
                AggregationKey    = aggregationKey,
                SourceType        = sourceType,
                DateHappened      = dateHappened,
                Priority          = priority,
                Hostname          = hostname,
                TruncateIfTooLong = truncateIfTooLong,
                Tags              = tags,
            };

            var serializedMetric = new SerializedMetric();

            serializer.SerializeTo(ref statsEvent, serializedMetric);
            Assert.AreEqual(expectValue, serializedMetric.ToString());
        }
        private static void AssertSerialize(
            string expectValue,
            ref StatsMetric statsMetric,
            string prefix)
        {
            var serializerHelper = new SerializerHelper(null);
            var serializer       = new MetricSerializer(serializerHelper, prefix);
            var serializedMetric = new SerializedMetric();

            serializer.SerializeTo(ref statsMetric, serializedMetric);
            Assert.AreEqual(expectValue, serializedMetric.ToString());
        }
예제 #3
0
        public void Add(SerializedMetric serializedMetric)
        {
            var length = serializedMetric.CopyToChars(_charsBuffers);

            if (length < 0)
            {
                throw new InvalidOperationException($"The metric size exceeds the internal buffer capacity {_charsBuffers.Length}: {serializedMetric.ToString()}");
            }

            var byteCount = _encoding.GetByteCount(_charsBuffers, 0, length);

            if (byteCount > Capacity)
            {
                throw new InvalidOperationException($"The metric size exceeds the buffer capacity {Capacity}: {serializedMetric.ToString()}");
            }

            if (Length != 0)
            {
                byteCount += _separator.Length;
            }

            if (Length + byteCount > Capacity)
            {
                this.HandleBufferAndReset();
            }

            if (Length != 0)
            {
                Array.Copy(_separator, 0, _buffer, Length, _separator.Length);
                Length += _separator.Length;
            }

            // GetBytes requires the buffer to be big enough otherwise it throws, that is why we use GetByteCount.
            Length += _encoding.GetBytes(_charsBuffers, 0, length, _buffer, Length);
        }