예제 #1
0
        public void TryConvert_AnonymousType_ReturnsStructureToken()
        {
            var logger = Mock.Of <ILogger>();
            var policy = new StructureConversionPolicy(logger);
            var value  = new
            {
                Foo = Some.String(),
                Bar = Some.Int()
            };

            var converterMock = new Mock <IPropertyConverter>();

            converterMock.Setup(m => m.Convert(It.IsAny <object>())).Returns <object>(o => new ScalarToken(o));
            var converter = converterMock.Object;

            IPropertyToken token;

            Assert.True(policy.TryConvert(converter, value, out token));

            var structure = token as StructureToken;

            Assert.NotNull(structure);
            Assert.Null(structure.TypeName);
            Assert.Equal(2, structure.Properties.Count);
        }
예제 #2
0
        public void CapturesTraceDataWithMultipleData()
        {
            var data1 = new
            {
                Info = Some.String()
            };
            var data2 = new
            {
                Info = Some.String()
            };
            var data3 = new
            {
                Info = Some.Int()
            };

            _traceListener.TraceData(_traceEventCache, _source, WarningEventType, _id, data1, data2, data3);

            // The square-brackets ('[' ,']') are because of serilog behavior and are not how the stock TraceListener would behave.
            LogEventAssert.HasMessage(
                string.Format("[\"{0}\"]", string.Join("\", \"", data1, data2, data3)),
                _loggedEvent);

            LogEventAssert.HasLevel(LogEventLevel.Warning, _loggedEvent);

            LogEventAssert.HasPropertyValue(_id, "TraceEventId", _loggedEvent);
            LogEventAssert.HasPropertyValue(_source, "TraceSource", _loggedEvent);
            LogEventAssert.HasPropertyValueSequenceValue(new object[]
            {
                data1,
                data2,
                data3
            }, "TraceData", _loggedEvent);
            LogEventAssert.HasPropertyValue(WarningEventType, "TraceEventType", _loggedEvent);
        }
예제 #3
0
        public void AScalarValueToStringRendersTheValue()
        {
            var num   = Some.Int();
            var value = _converter.CreatePropertyValue(num, Destructuring.Default);
            var str   = value.ToString();

            Assert.AreEqual(num.ToString(CultureInfo.InvariantCulture), str);
        }
예제 #4
0
        public void IsCompilerGenerated_AnonymousType_ReturnsTrue()
        {
            var foobar = new
            {
                Foo = Some.String(),
                Bar = Some.Int()
            };

            Assert.True(foobar.GetType().IsCompilerGenerated());
        }
예제 #5
0
        public void AnIntegerPropertySerializesAsIntegerValue()
        {
            var name = Some.String();
            var value = Some.Int();
            var @event = Some.InformationEvent();
            @event.AddOrUpdateProperty(new LogEventProperty(name, new ScalarValue(value)));

            var formatted = FormatJson(@event);

            Assert.AreEqual(value, (int)formatted.Properties[name]);
        }
        public void Format_IntScalarToken_IsFormatedAsNumber()
        {
            var     output   = new StringWriter();
            var     formater = new JsonPropertyFormatter(output);
            decimal value    = Some.Int();
            var     token    = new ScalarToken(value);

            token.Render(formater);

            Assert.Equal(value.ToString(CultureInfo.InvariantCulture), output.ToString());
        }
예제 #7
0
        public void PropertiesInANestedContextOverrideParentContextValues()
        {
            var name = Some.String();
            var v1 = Some.Int();
            var v2 = Some.Int();
            var evt = DelegatingSink.GetLogEvent(l => l.ForContext(name, v1)
                .ForContext(name, v2)
                .Write(Some.InformationEvent()));

            var pActual = evt.Properties[name];
            Assert.Equal(v2, pActual.LiteralValue());
        }
예제 #8
0
        public void AStructureSerializesAsAnObject()
        {
            var value = Some.Int();
            var memberProp = new LogEventProperty(Some.String(), new ScalarValue(value));
            var structure = new StructureValue(new[] { memberProp });
            var structureProp = new LogEventProperty(Some.String(), structure);
            var @event = Some.InformationEvent();
            @event.AddOrUpdateProperty(structureProp);

            var formatted = FormatJson(@event);
            var result = (int)formatted.Properties[structureProp.Name][memberProp.Name];
            Assert.AreEqual(value, result);
        }
예제 #9
0
        public void ASequencePropertySerializesAsArrayValue()
        {
            var name = Some.String();
            var ints = new[]{ Some.Int(), Some.Int() };
            var value = new SequenceValue(ints.Select(i => new ScalarValue(i)));
            var @event = Some.InformationEvent();
            @event.AddOrUpdateProperty(new LogEventProperty(name, value));

            var formatted = FormatJson(@event);
            var result = new List<int>();
            foreach (var el in formatted.Properties[name])
                result.Add((int)el);

            CollectionAssert.AreEqual(ints, result);
        }
예제 #10
0
        public void CorrectlyOrdersFormatArgs()
        {
            const string format = "{1}-{0}-{1}";
            var          args   = new object[]
            {
                Some.Int(),
                         Some.Int(),
                         Some.Int()
            };

            _traceListener.TraceEvent(_traceEventCache, _source, WarningEventType, _id, format, args);

            LogEventAssert.HasMessage(args[1].ToString() + "-" + args[0].ToString() + "-" + args[1].ToString(), _loggedEvent);
            LogEventAssert.HasPropertyValue(args[2], "2", _loggedEvent);
        }
예제 #11
0
        public void WriteOfObjectHasTraceDataProperty()
        {
            var writtenObject = new int[] {
                Some.Int(),
                Some.Int()
            };

            _traceListener.Write(writtenObject);

            LogEventAssert.HasMessage(string.Format("[{0}, {1}]", writtenObject[0], writtenObject[1]), _loggedEvent);
            LogEventAssert.HasLevel(LogEventLevel.Debug, _loggedEvent);

            var sequence = ((SequenceValue)_loggedEvent.Properties["TraceData"]).Elements.Select(pv => pv.LiteralValue());

            Assert.That(sequence, Is.EquivalentTo(writtenObject), "The property value was not as expected");
        }
예제 #12
0
        public void ADictionaryWithScalarKeySerializesAsAnObject()
        {
            var dictKey   = Some.Int();
            var dictValue = Some.Int();
            var dict      = new DictionaryValue(new Dictionary <ScalarValue, LogEventPropertyValue> {
                { new ScalarValue(dictKey), new ScalarValue(dictValue) }
            });
            var dictProp = new LogEventProperty(Some.String(), dict);
            var @event   = Some.InformationEvent();

            @event.AddOrUpdateProperty(dictProp);

            var formatted = FormatToJson(@event);
            var expected  = $"{{\"{dictKey}\":{dictValue}}}";

            Assert.Contains(expected, formatted);
        }
예제 #13
0
        public void HandlesNullFormatString()
        {
            const string format = null;
            var          args   = new object[]
            {
                Some.Int(),
                         Some.Int(),
                         Some.Int()
            };

            _traceListener.TraceEvent(_traceEventCache, _source, WarningEventType, _id, format, args);

            LogEventAssert.HasMessage(string.Empty, _loggedEvent);
            LogEventAssert.HasPropertyValue(args[0], "0", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[1], "1", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[2], "2", _loggedEvent);
        }
        public void HandlesNullFormatString()
        {
            var args = new object[]
            {
                Some.Int(),
                Some.Int(),
                Some.Int()
            };

            // ReSharper disable once AssignNullToNotNullAttribute
            _traceListener.TraceEvent(_traceEventCache, _source, WarningEventType, _id, null, args);

            LogEventAssert.HasMessage(string.Empty, _loggedEvent);
            LogEventAssert.HasPropertyValue(args[0], "0", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[1], "1", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[2], "2", _loggedEvent);
        }
예제 #15
0
        public void CapturesTraceEventWithFormatMessage()
        {
            const string format = "{0}-{1}-{2}";
            var          args   = new object[]
            {
                Some.Int(),
                         Some.Int(),
                         Some.Int()
            };

            _traceListener.TraceEvent(_traceEventCache, _source, WarningEventType, _id, format, args);

            LogEventAssert.HasMessage(string.Format(format, args[0], args[1], args[2]), _loggedEvent);
            LogEventAssert.HasLevel(LogEventLevel.Warning, _loggedEvent);
            LogEventAssert.HasPropertyValue(_id, "TraceEventId", _loggedEvent);
            LogEventAssert.HasPropertyValue(_source, "TraceSource", _loggedEvent);
            LogEventAssert.HasPropertyValue(WarningEventType, "TraceEventType", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[0], "0", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[1], "1", _loggedEvent);
            LogEventAssert.HasPropertyValue(args[2], "2", _loggedEvent);
        }
예제 #16
0
        public void CapturesTraceDataArrayOfInt()
        {
            var data = new int[]
            {
                Some.Int(),
                Some.Int()
            };

            _traceListener.TraceData(_traceEventCache, _source, WarningEventType, _id, data);

            LogEventAssert.HasMessage(
                String.Format("[{0}, {1}]", data[0], data[1]),
                _loggedEvent);

            LogEventAssert.HasLevel(LogEventLevel.Warning, _loggedEvent);

            LogEventAssert.HasPropertyValue(_id, "TraceEventId", _loggedEvent);
            LogEventAssert.HasPropertyValue(_source, "TraceSource", _loggedEvent);

            var sequence = ((SequenceValue)_loggedEvent.Properties["TraceData"]).Elements.Select(pv => pv.LiteralValue());

            Assert.That(sequence, Is.EquivalentTo(data), "The property value was not as expected");
        }