Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
if (logger.BindProperty(property.Key.Substring(1), property.Value, true, out var destructured))
properties.Add(destructured);
}
else if (property.Key.StartsWith("$"))
{
if (logger.BindProperty(property.Key.Substring(1), property.Value?.ToString() ?? "null", true, out var stringified))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Substituting in the text null will be inconsistent with Serilog here, I think. We should just pass null if property.Value is null.

properties.Add(stringified);
}
else
{
if (logger.BindProperty(property.Key, property.Value, false, out var bound))
Expand Down
18 changes: 18 additions & 0 deletions test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ public void CarriesMessageTemplateProperties()
Assert.Empty(selfLog.ToString());
}

[Fact]
public void CarriesMessageTemplatePropertiesWhenStringificationIsUsed()
{
var selfLog = new StringWriter();
SelfLog.Enable(selfLog);
var (logger, sink) = SetUp(LogLevel.Trace);
var array = new[] { 1, 2, 3, 4 };

logger.LogInformation("{$array}", array);

Assert.True(sink.Writes[0].Properties.ContainsKey("array"));
Assert.Equal("\"System.Int32[]\"", sink.Writes[0].Properties["array"].ToString());
Assert.Equal("{$array}", sink.Writes[0].MessageTemplate.Text);

SelfLog.Disable();
Assert.Empty(selfLog.ToString());
}

[Fact]
public void CarriesEventIdIfNonzero()
{
Expand Down