private string Render()
        {
            var stringBuilder = new StringBuilder();
            var stringWriter  = new StringWriter(stringBuilder);

            token.Render(@event, stringWriter, null);

            return(stringBuilder.ToString());
        }
        public void Render_PropertyToken_IsFormattedAsProperty()
        {
            var output   = new StringWriter();
            var formater = new JsonPropertyFormatter(output);
            var property = new PropertyToken("property", new ScalarToken(1));

            property.Render(formater);

            Assert.Equal("\"property\": 1", output.ToString());
        }
Exemplo n.º 3
0
            public void Should_Format_Argument_As_String_When_No_Formatting_Rules_Specified()
            {
                // Given
                var token = new PropertyToken(0, null);

                // When
                var result = token.Render(new object[] { new Guid("{d6ed7358-ef96-45bf-9245-864025de28fa}") });

                // Then
                Assert.Equal("d6ed7358-ef96-45bf-9245-864025de28fa", result);
            }
Exemplo n.º 4
0
            public void Should_Format_Argument_According_To_Formatting_Rules()
            {
                // Given
                var token = new PropertyToken(0, "B");

                // When
                var result = token.Render(new object[] { new Guid("d6ed7358ef9645bf9245864025de28fa") });

                // Then
                Assert.Equal("{d6ed7358-ef96-45bf-9245-864025de28fa}", result);
            }
Exemplo n.º 5
0
        public void Render_SomeFormatter_FormatterIsCalled()
        {
            var formatterMock = new Mock <IPropertyFormatter>();

            var formatter = formatterMock.Object;
            var property  = new PropertyToken(Some.String(), Mock.Of <IPropertyToken>());

            property.Render(formatter);

            formatterMock.Verify(m => m.Format(property), Times.Once);
        }
Exemplo n.º 6
0
            public void Should_Throw_FormatException_When_Index_And_Args_Are_Mismatched()
            {
                // Given
                var token = new PropertyToken(1, null);

                // When
                var ex = Record.Exception(() => token.Render(new object[] { "test" }));

                // Then
                Assert.IsType <FormatException>(ex);
                Assert.Equal("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.", ex.Message);
            }
Exemplo n.º 7
0
        void RenderOutputTemplatePropertyToken(
            PropertyToken outputToken,
            IReadOnlyDictionary <string, LogEventPropertyValue> outputProperties,
            TextWriter outputStream)
        {
            SysConsole.ForegroundColor = Subtext;

            // This code is shared with MessageTemplateFormatter in the core Serilog
            // project. Its purpose is to modify the way tokens are formatted to
            // use "output template" rather than "message template" rules.

            // First variation from normal rendering - if a property is missing,
            // don't render anything (message templates render the raw token here).
            LogEventPropertyValue propertyValue;

            if (!outputProperties.TryGetValue(outputToken.PropertyName, out propertyValue))
            {
                return;
            }

            // Second variation; if the value is a scalar string, use literal
            // rendering and support some additional formats: 'u' for uppercase
            // and 'w' for lowercase.
            var sv = propertyValue as ScalarValue;

            if (sv?.Value is string)
            {
                var overridden = new Dictionary <string, LogEventPropertyValue>
                {
                    { outputToken.PropertyName, new LiteralStringValue((string)sv.Value) }
                };

                outputToken.Render(overridden, outputStream, _formatProvider);
            }
            else
            {
                outputToken.Render(outputProperties, outputStream, _formatProvider);
            }
        }
        void RenderExceptionToken(
            PropertyToken outputToken,
            IReadOnlyDictionary <string, LogEventPropertyValue> outputProperties)
        {
            var sw = new StringWriter();

            outputToken.Render(outputProperties, sw, _formatProvider);
            var    lines = new StringReader(sw.ToString());
            string nextLine;

            while ((nextLine = lines.ReadLine()) != null)
            {
                Console.ForegroundColor = nextLine.StartsWith(StackFrameLinePrefix) ? Subtext : Text;
                Console.WriteLine(nextLine);
            }
        }
Exemplo n.º 9
0
        public void Render_NullFormatter_ThrowsNullArgumentException()
        {
            var property = new PropertyToken(Some.String(), Mock.Of <IPropertyToken>());

            Assert.Throws <ArgumentNullException>(() => property.Render(null));
        }