Exemplo n.º 1
0
 /// <summary>
 /// Convert the message template into a textual message, given the
 /// properties matching the tokens in the message template.
 /// </summary>
 /// <param name="properties">Properties matching template tokens.</param>
 /// <param name="output">The message created from the template and properties. If the
 /// properties are mismatched with the template, the template will be
 /// returned with incomplete substitution.</param>
 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 public void Render(IReadOnlyDictionary <string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null)
 {
     if (properties == null)
     {
         throw new ArgumentNullException(nameof(properties));
     }
     if (output == null)
     {
         throw new ArgumentNullException(nameof(output));
     }
     MessageTemplateRenderer.Render(this, properties, output, null, formatProvider);
 }
Exemplo n.º 2
0
        public void PropertyTokenFormatsAreApplied(string template, string appliedFormat, string expected)
        {
            var eventTemplate = _messageTemplateParser.Parse(template);
            var properties    = new Dictionary <string, LogEventPropertyValue> {
                ["Number"] = new ScalarValue(16)
            };

            var output = new StringWriter();

            MessageTemplateRenderer.Render(eventTemplate, properties, output, appliedFormat, CultureInfo.InvariantCulture);

            Assert.Equal(expected, output.ToString());
        }
        /// <summary>
        /// Format the log event into the output.
        /// </summary>
        /// <param name="logEvent">The event to format.</param>
        /// <param name="output">The output.</param>
        public void Format(LogEvent logEvent, TextWriter output)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            foreach (var token in _outputTemplate.Tokens)
            {
                if (token is TextToken tt)
                {
                    MessageTemplateRenderer.RenderTextToken(tt, output);
                    continue;
                }

                var pt = (PropertyToken)token;
                if (pt.PropertyName == OutputProperties.LevelPropertyName)
                {
                    var moniker = LevelOutputFormat.GetLevelMoniker(logEvent.Level, pt.Format);
                    Padding.Apply(output, moniker, pt.Alignment);
                }
                else if (pt.PropertyName == OutputProperties.NewLinePropertyName)
                {
                    Padding.Apply(output, Environment.NewLine, pt.Alignment);
                }
                else if (pt.PropertyName == OutputProperties.ExceptionPropertyName)
                {
                    var exception = logEvent.Exception == null ? "" : logEvent.Exception + Environment.NewLine;
                    Padding.Apply(output, exception, pt.Alignment);
                }
                else
                {
                    // In this block, `writer` may be used to buffer output so that
                    // padding can be applied.
                    var writer = pt.Alignment.HasValue ? new StringWriter() : output;

                    if (pt.PropertyName == OutputProperties.MessagePropertyName)
                    {
                        MessageTemplateRenderer.Render(logEvent.MessageTemplate, logEvent.Properties, writer, pt.Format, _formatProvider);
                    }
                    else if (pt.PropertyName == OutputProperties.TimestampPropertyName)
                    {
                        ScalarValue.Render(logEvent.Timestamp, writer, pt.Format, _formatProvider);
                    }
                    else if (pt.PropertyName == OutputProperties.PropertiesPropertyName)
                    {
                        PropertiesOutputFormat.Render(logEvent.MessageTemplate, logEvent.Properties, _outputTemplate, writer, pt.Format, _formatProvider);
                    }
                    else
                    {
                        // If a property is missing, don't render anything (message templates render the raw token here).
                        if (!logEvent.Properties.TryGetValue(pt.PropertyName, out var propertyValue))
                        {
                            continue;
                        }

                        // If the value is a scalar string, support some additional formats: 'u' for uppercase
                        // and 'w' for lowercase.
                        var sv = propertyValue as ScalarValue;
                        if (sv?.Value is string literalString)
                        {
                            var cased = Casing.Format(literalString, pt.Format);
                            writer.Write(cased);
                        }
                        else
                        {
                            propertyValue.Render(writer, pt.Format, _formatProvider);
                        }
                    }

                    if (pt.Alignment.HasValue)
                    {
                        Padding.Apply(output, ((StringWriter)writer).ToString(), pt.Alignment);
                    }
                }
            }
        }