Example #1
0
        private static (List <IFormatter> fieldFormatters, List <IFormatter> tagFormatters) CreateFormatters(Type type,
                                                                                                             CustomDict customFieldFormatters, CustomDict customTagFormatters)
        {
            var fieldFormatters = new List <IFormatter>();
            var tagFormatters   = new List <IFormatter>();

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

                if (FieldFormatter.IsFieldType(property.PropertyType))
                {
                    var formatter = FieldFormatter.TryCreate(property);
                    if (formatter != null)
                    {
                        fieldFormatters.Add(formatter);
                    }
                }
                else if (TagFormatter.IsTagType(property.PropertyType))
                {
                    var tagFormatter = TagFormatter.TryCreate(property);
                    if (tagFormatter != null)
                    {
                        tagFormatters.Add(tagFormatter);
                    }
                }
            }

            return(fieldFormatters, tagFormatters);
        }
Example #2
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);
        }
        public void FormatsDecimalCorrectly()
        {
            var args      = new { foo = 42m };
            var formatter = FieldFormatter.TryCreate(args.GetType().GetProperty("foo"), NameFixer.Identity);
            var buffer    = new byte[64];

            Assert.True(formatter.TryWrite(args, buffer.AsSpan(), false, out int written));
            Assert.Equal(6, written);
            var text = Encoding.UTF8.GetString(buffer, 0, written);

            Assert.Equal("foo=42", text);
        }
Example #4
0
        public void FormatsUInt32(Func <string, string> formatter)
        {
            var obj      = new { foo = 42u };
            var property = obj.GetType().GetProperty("foo");
            var target   = FieldFormatter.TryCreate(property, formatter);
            var bytes    = new byte[7];
            var span     = bytes.AsSpan();

            Assert.True(target.TryWrite(obj, span, false, out int bytesWritten));
            Assert.Equal(7, bytesWritten);
            Assert.Equal(Encoding.UTF8.GetBytes($"{formatter("foo")}=42i"), bytes);
        }