Skip to content

Commit

Permalink
Handle null scope values in EventLogLogger (dotnet/extensions#1850)
Browse files Browse the repository at this point in the history
- Added test


Commit migrated from dotnet/extensions@3d0c881
  • Loading branch information
davidfowl authored Jun 15, 2019
1 parent 8b07c9a commit 57f49f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ public void Log<TState>(
{
foreach (var pair in properties)
{
sb.Append(pair.Key).Append(": ").AppendLine(pair.Value.ToString());
sb.Append(pair.Key).Append(": ").AppendLine(pair.Value?.ToString());
}
}
else
else if (scope != null)
{
sb.AppendLine(scope.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,26 @@ public void Message_WritesFullMessageWithScopes()
var expectedMessage = "Category: " + loggerName + Environment.NewLine +
"EventId: 0" + Environment.NewLine +
"Outer Scope" + Environment.NewLine +
"Inner Scope" + Environment.NewLine + Environment.NewLine +
"Inner Scope" + Environment.NewLine +
"K1: V1" + Environment.NewLine +
"K2: " + Environment.NewLine +
"K3: " + Environment.NewLine +
Environment.NewLine +
"Message" + Environment.NewLine;
var testEventLog = new TestEventLog(expectedMessage.Length);
var logger = new EventLogLogger(loggerName, new EventLogSettings() { EventLog = testEventLog }, new LoggerExternalScopeProvider());
var scopeWithValues = new Dictionary<string, object>
{
{ "K1", "V1" },
{ "K2", "" },
{ "K3", null },
};

// Act
using (logger.BeginScope("Outer Scope"))
using (logger.BeginScope("Inner Scope"))
using (logger.BeginScope(scopeWithValues))
using (logger.BeginScope((object)null))
{
logger.LogInformation("Message");
}
Expand Down

0 comments on commit 57f49f7

Please sign in to comment.