Exemplo n.º 1
0
        public ObjectFormatter(Type type, DiagnosticListenerOptions options)
        {
            var(fieldFormatters, tagFormatters) = CreateFormatters(type, options);

            _fieldFormatters = fieldFormatters.ToArray();
            _fieldCount      = _fieldFormatters.Length;
            _tagFormatters   = tagFormatters.ToArray();
            _tagCount        = _tagFormatters.Length;
        }
        public void AddsDefaultTags()
        {
            var options = new DiagnosticListenerOptions();

            options.AddDefaultTag("foo", "one");
            options.AddDefaultTag("bar", "two");
            var text = Encoding.UTF8.GetString(options.DefaultTags);

            Assert.Equal(",foo=one,bar=two", text);
        }
        public void FormatsCustomField()
        {
            var args = new { custom = new CustomObject {
                                 Value = 42, Thing = "foo"
                             } };
            var options = new DiagnosticListenerOptions();

            options.AddCustomFieldFormatter("custom", typeof(CustomObject), p => new CustomObjectFieldFormatter(p));
            var objectFormatter = new ObjectFormatter(args.GetType(), options);
            var buffer          = new byte[128];
            var span            = buffer.AsSpan();

            Assert.True(objectFormatter.Write(args, null, span, out int written));
            var text = Encoding.UTF8.GetString(buffer, 0, written);

            Assert.Equal(" v=42", text);
        }
        public void WritesDefaultTags()
        {
            var now      = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            var expected = $"test,foo=bar,tag=foo foo=42i {now}\n";
            var options  = new DiagnosticListenerOptions();

            options.AddDefaultTag("foo", "bar");

            var obj       = new { tag = "foo", foo = 42 };
            var formatter = new InfluxLineWriter("test", obj.GetType(), options);

            var memory = ArrayPool <byte> .Shared.Rent(1024);

            var span = memory.AsSpan();

            formatter.TryWrite(span, obj, null, now, out int written);
            var str = Encoding.UTF8.GetString(memory, 0, written);

            Assert.Equal(expected, str);
            ArrayPool <byte> .Shared.Return(memory);
        }
        public void WritesTagsAndFields_WithFormattedNames()
        {
            var now      = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            var expected = $"test,TAG=foo FOO=42i {now}\n";
            var options  = new DiagnosticListenerOptions();

            options.TagNameFormatter   = n => n.ToUpperInvariant();
            options.FieldNameFormatter = n => n.ToUpperInvariant();

            var obj       = new { tag = "foo", foo = 42 };
            var formatter = new InfluxLineWriter("test", obj.GetType(), options);

            var memory = ArrayPool <byte> .Shared.Rent(1024);

            var span = memory.AsSpan();

            formatter.TryWrite(span, obj, null, now, out int written);
            var str = Encoding.UTF8.GetString(memory, 0, written);

            Assert.Equal(expected, str);
            ArrayPool <byte> .Shared.Return(memory);
        }
        public void WritesTagsAndFields_WithFormattedNames()
        {
            const string expected = ",TAG=foo FOO=42i";
            var          options  = new DiagnosticListenerOptions();

            options.TagNameFormatter   = n => n.ToUpperInvariant();
            options.FieldNameFormatter = n => n.ToUpperInvariant();

            var obj       = new { tag = "foo", foo = 42 };
            var formatter = new ObjectFormatter(obj.GetType(), options);

            var memory = ArrayPool <byte> .Shared.Rent(1024);

            var span = memory.AsSpan();

            formatter.Write(obj, null, span, out int written);
            var str = Encoding.UTF8.GetString(memory, 0, written);

            Assert.Equal(expected.Length, written);
            Assert.Equal(expected, str);
            ArrayPool <byte> .Shared.Return(memory);
        }
Exemplo n.º 7
0
        private static (List <IFormatter> fieldFormatters, List <IFormatter> tagFormatters) CreateFormatters(Type type,
                                                                                                             DiagnosticListenerOptions options)
        {
            var fieldFormatters = new List <IFormatter>();
            var tagFormatters   = new List <IFormatter>();

            foreach (var property in type.GetProperties().Where(p => p.CanRead))
            {
                if (CheckCustomFormatters(options.CustomFieldFormatters, options.CustomTagFormatters, property, fieldFormatters, tagFormatters))
                {
                    continue;
                }

                if (FieldFormatter.IsFieldType(property.PropertyType))
                {
                    var formatter = FieldFormatter.TryCreate(property, options.FieldNameFormatter ?? NameFixer.Identity);
                    if (formatter != null)
                    {
                        fieldFormatters.Add(formatter);
                    }
                }
                else if (TagFormatter.IsTagType(property.PropertyType))
                {
                    var tagFormatter = TagFormatter.TryCreate(property, options.TagNameFormatter ?? NameFixer.Identity);
                    if (tagFormatter != null)
                    {
                        tagFormatters.Add(tagFormatter);
                    }
                }
            }

            return(fieldFormatters, tagFormatters);
        }