示例#1
0
        public void FormatProviderWithDestructuredProperties(string format, bool shouldUseCustomFormatter)
        {
            var frenchFormatProvider  = new CultureInfo("fr-FR");
            var defaultFormatProvider = CultureInfo.InvariantCulture;

            var date   = new DateTime(2018, 01, 01);
            var number = 12.345;

            var expectedFormattedDate = shouldUseCustomFormatter
                ? date.ToString(frenchFormatProvider)
                : date.ToString("O", defaultFormatProvider);
            var expectedFormattedNumber = shouldUseCustomFormatter
                ? number.ToString(frenchFormatProvider)
                : number.ToString(defaultFormatProvider);

            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Message" + format + "}", frenchFormatProvider);
            var evt       = DelegatingSink.GetLogEvent(
                l => {
                l.Information(
                    "{@Item}", new {
                    MyDate   = date,
                    MyNumber = number,
                }
                    );
            }
                );
            var sw = new StringWriter();

            formatter.Format(evt, sw);

            Assert.Contains(expectedFormattedDate, sw.ToString());
            Assert.Contains(expectedFormattedNumber, sw.ToString());
        }
示例#2
0
        public void AContextualLoggerAddsTheSourceTypeName()
        {
            var evt = DelegatingSink.GetLogEvent(l => l.ForContext<LoggerTests>()
                .Information(Some.String()));

            var lv = evt.Properties[Constants.SourceContextPropertyName].LiteralValue();
            Assert.Equal(typeof(LoggerTests).FullName, lv);
        }
        public void AppliesLiteralFormattingToMessageStringsWhenSpecified(string format, string expected)
        {
            var formatter = new MessageTemplateTextFormatter("{Message" + format + "}", null);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello, {Name}!", "World"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal(expected, sw.ToString());
        }
        public void NonMessagePropertiesAreRendered()
        {
            var formatter = new MessageTemplateTextFormatter("{Properties}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.ForContext("Foo", 42).Information("Hello from {Bar}!", "bar"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("{ Foo: 42 }", sw.ToString());
        }
示例#5
0
        public void LowercaseFormatSpecifierIsSupportedForStrings()
        {
            var formatter = new MessageTemplateTextFormatter("{Name:w}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{Name}", "Nick"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("nick", sw.ToString());
        }
示例#6
0
        public void FixedLengthLevelSupportsUpperCasing()
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Level:u3}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("INF", sw.ToString());
        }
示例#7
0
        public void AppliesJsonFormattingToPropertiesTokenWhenSpecified(string format, string expected)
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Properties" + format + "}", null);
            var evt       = DelegatingSink.GetLogEvent(l => l.ForContext("Name", "World").Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal(expected, sw.ToString());
        }
        public void AppliesCustomFormatterToEnums()
        {
            var formatter = new MessageTemplateTextFormatter("{Message}", new SizeFormatter(CultureInfo.InvariantCulture));
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Size {Size}", Size.Large));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("Size Huge", sw.ToString());
        }
示例#9
0
        public void DefaultLevelLengthIsFullText()
        {
            var formatter = new MessageTemplateTextFormatter("{Level}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("Information", sw.ToString());
        }
示例#10
0
        public void DoNotDuplicatePropertiesAlreadyRenderedInOutputTemplate()
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Foo} {Properties}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.ForContext("Foo", 42).ForContext("Bar", 42).Information("Hello from bar!"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("42 {Bar=42}", sw.ToString());
        }
示例#11
0
        public void UppercaseFormatSpecifierIsSupportedForStrings()
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Name:u}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{Name}", "Nick"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("NICK", sw.ToString());
        }
示例#12
0
        public void AligmentAndWidthCanBeCombined()
        {
            var formatter = new MessageTemplateTextFormatter("{Level,5:w3}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("  inf", sw.ToString());
        }
示例#13
0
        public void AppliesJsonFormattingToMessageStructuresWhenSpecified(string format, string expected)
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Message" + format + "}", null);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{@Obj}", new { Name = "World" }));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal(expected, sw.ToString());
        }
示例#14
0
        public void NonMessagePositionalPropertiesAreRendered()
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Properties}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.ForContext("Foo", 42).Information("Hello from {0}!", "bar"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("{Foo=42}", sw.ToString());
        }
示例#15
0
        public void FixedLengthLevelSupportsLowerCasing()
        {
            var formatter = new MessageTemplateTextFormatter("{Level:w3}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("inf", sw.ToString());
        }
示例#16
0
        public void MessageTemplatesContainingFormatStringPropertiesRenderCorrectly()
        {
            var formatter = new MessageTemplateTextFormatter("{Message}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{Message}", "Hello, world!"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("\"Hello, world!\"", sw.ToString());
        }
示例#17
0
        public void UsesFormatProvider()
        {
            var french    = new CultureInfo("fr-FR");
            var formatter = new MessageTemplateTextFormatter("{Message}", french);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{0}", 12.345));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal("12,345", sw.ToString());
        }
        public void AlignmentAndWidthCanBeCombined()
        {
            var formatter = new XamlOutputTemplateRenderer(RichTextBoxTheme.None, "{Level,5:w3}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));

            var sw = new StringWriter();

            formatter.Format(evt, sw);

            Assert.Equal("  inf", sw.ToString());
        }
        public void DefaultLevelLengthIsFullText()
        {
            var formatter = new XamlOutputTemplateRenderer(RichTextBoxTheme.None, "{Level}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));

            var sw = new StringWriter();

            formatter.Format(evt, sw);

            Assert.Equal("Information", sw.ToString());
        }
示例#20
0
        public void FixedLengthLevelIsSupported(LogEventLevel level,
                                                int width,
                                                string expected)
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, $"{{Level:t{width}}}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Write(level, "Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);
            Assert.Equal(expected, sw.ToString());
        }
        public void LowercaseFormatSpecifierIsSupportedForStrings()
        {
            var formatter = new XamlOutputTemplateRenderer(RichTextBoxTheme.None, "{Name:w}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("{Name}", "Nick"));

            var sw = new StringWriter();

            formatter.Format(evt, sw);

            Assert.Equal("nick", sw.ToString());
        }
        public void FixedLengthLevelSupportsLowerCasing()
        {
            var formatter = new XamlOutputTemplateRenderer(RichTextBoxTheme.None, "{Level:w3}", CultureInfo.InvariantCulture);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));

            var sw = new StringWriter();

            formatter.Format(evt, sw);

            Assert.Equal("inf", sw.ToString());
        }
        public void AnEmptyPropertiesTokenIsAnEmptyStructureValueWithDefaultFormatting()
        {
            var formatter = new MessageTemplateTextFormatter("{Properties}", null);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);

            var expected = new StructureValue(Enumerable.Empty <LogEventProperty>()).ToString();

            Assert.Equal(expected, sw.ToString());
        }
示例#24
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());
        }
示例#25
0
        public void DictionariesAreSerialisedAsObjects()
        {
            var dict = new Dictionary <string, object> {
                { "hello", "world" },
                { "nums", new[] { 1.2 } }
            };

            var e = DelegatingSink.GetLogEvent(l => l.Information("Value is {ADictionary}", dict));
            var f = FormatJson(e);

            Assert.AreEqual("world", (string)f.Properties.ADictionary["hello"]);
            Assert.AreEqual(1.2, (double)f.Properties.ADictionary.nums[0]);
        }
示例#26
0
        public void DictionariesAreSerialisedAsObjects()
        {
            var dict = new DictionaryValue(new[] {
                new KeyValuePair<ScalarValue, LogEventPropertyValue>(
                    new ScalarValue(1), new ScalarValue("hello")),
                new KeyValuePair<ScalarValue, LogEventPropertyValue>(
                    new ScalarValue("world"), new SequenceValue(new [] { new ScalarValue(1.2)  }))
            });

            var e = DelegatingSink.GetLogEvent(l => l.Information("Value is {ADictionary}", dict));
            var f = FormatJson(e);

            Assert.AreEqual("hello", (string)f.Properties.ADictionary["1"]);
            Assert.AreEqual(1.2, (double)f.Properties.ADictionary.world[0]);
        }
示例#27
0
        public void AnEmptyPropertiesTokenIsAnEmptyStructureValue()
        {
            var formatter = new OutputTemplateRenderer(DefaultThemes.None, "{Properties}", null);
            var evt       = DelegatingSink.GetLogEvent(l => l.Information("Hello"));
            var sw        = new StringWriter();

            formatter.Format(evt, sw);

            // /!\ different behavior from Serilog Core : https://github.com/serilog/serilog/blob/5c3a7821aa0f654e551dc21e8e19089f6767666b/test/Serilog.Tests/Formatting/Display/MessageTemplateTextFormatterTests.cs#L268-L278
            //
            // var expected = new StructureValue(Enumerable.Empty<LogEventProperty>()).ToString();
            // // expected == "{  }"
            // Assert.Equal(expected, sw.ToString());
            //
            Assert.Equal("{}", sw.ToString());
        }
示例#28
0
        public void ParametersForAnEmptyTemplateAreIgnored()
        {
            var e = DelegatingSink.GetLogEvent(l => l.Error("message", new object()));

            Assert.Equal("message", e.RenderMessage());
        }