public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null) { output.Write(Casing.Format(_value, format)); }
/// <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). LogEventPropertyValue propertyValue; if (!logEvent.Properties.TryGetValue(pt.PropertyName, out 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(output, pt.Format, _formatProvider); } } if (pt.Alignment.HasValue) { Padding.Apply(output, ((StringWriter)writer).ToString(), pt.Alignment); } } } }