Skip to content

Commit

Permalink
Merge pull request #1325 from nblumhardt/default-format
Browse files Browse the repository at this point in the history
Respect explicit format specifiers even when JSON defaults are selected
  • Loading branch information
nblumhardt committed Jun 22, 2019
2 parents c2f78f0 + d953cee commit e6936a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Serilog/Rendering/MessageTemplateRenderer.cs
Expand Up @@ -95,7 +95,7 @@ static void RenderValue(LogEventPropertyValue propertyValue, bool literal, bool
{
output.Write(str);
}
else if (json)
else if (json && format == null)
{
JsonValueFormatter.Format(propertyValue, output);
}
Expand Down
32 changes: 32 additions & 0 deletions test/Serilog.Tests/Rendering/MessageTemplateRendererTests.cs
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Serilog.Events;
using Serilog.Parsing;
using Serilog.Rendering;
using Xunit;

namespace Serilog.Tests.Rendering
{
public class MessageTemplateRendererTests
{
readonly MessageTemplateParser _messageTemplateParser = new MessageTemplateParser();

[Theory]
[InlineData("{Number}", null, "16")]
[InlineData("{Number:X8}", null, "00000010")]
[InlineData("{Number}", "j", "16")]
[InlineData("{Number:X8}", "j", "00000010")]
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());
}
}
}

0 comments on commit e6936a1

Please sign in to comment.