Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added stringification support #160

Merged
merged 4 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
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(), true, out var stringified))
properties.Add(stringified);
}
else
{
if (logger.BindProperty(property.Key, property.Value, false, out var bound))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public void EnrichAndCreateScopeItem(LogEvent logEvent, ILogEventPropertyFactory
destructureObject = true;
}

if (key.StartsWith("$"))
{
key = key.Substring(1);
Copy link
Member

Choose a reason for hiding this comment

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

The value would also have to be ToString()ed in order for this to take effect.

Copy link
Author

Choose a reason for hiding this comment

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

sry, i was a little too fast. Fixed it again and added a unit test.

}

var property = propertyFactory.CreateProperty(key, stateProperty.Value, destructureObject);
logEvent.AddPropertyIfAbsent(property);
}
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