Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 7, 2023
1 parent 1203b62 commit 11b41b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,24 @@ public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) whe
}
if ((IncludeProperties & IncludeProperties.ScopeKeyValues) != 0)
{
if (entry.LogInfo.ScopeState != null)
if (entry.LogInfo.ScopeState == null)
{
propCount--;
}
else
{
var scopeProperties = entry.LogInfo.ScopeState.Properties;
foreach (var t in scopeProperties)
{
if (t.Key != "{OriginalFormat}") scopePropCount++;
}
}

// flatten
if (PropertyNames.ScopeKeyValues == null)
{
propCount--;
propCount += scopePropCount;
// flatten
if (PropertyNames.ScopeKeyValues == null)
{
propCount--;
propCount += scopePropCount;
}
}
}

Expand Down
35 changes: 34 additions & 1 deletion tests/ZLogger.MessagePack.Tests/FormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,46 @@ public void NestedParameterKeyValues()
logger = loggerFactory.CreateLogger("test");


logger.ZLogInformation($"{456:@y}");

var msg = processor.Dequeue();
((string)msg["Message"]).Should().Be("456");
((int)msg["attributes"]["y"]).Should().Be(456);
}

[Fact]
public void NestedScopeKeyValues()
{
var formatter = new MessagePackZLoggerFormatter
{
PropertyNames = MessagePackPropertyNames.Default with
{
ParameterKeyValues = MessagePackEncodedText.Encode("attributes"),
ScopeKeyValues = MessagePackEncodedText.Encode("scope")
}
};

processor = new TestProcessor(formatter);

var loggerFactory = LoggerFactory.Create(x =>
{
x.SetMinimumLevel(LogLevel.Debug);
x.AddZLoggerLogProcessor(options =>
{
options.IncludeScopes = true;
options.TimeProvider = timeProvider;
return processor;
});
});
logger = loggerFactory.CreateLogger("test");

using (logger.BeginScope("{X}", 123))
{
logger.ZLogInformation($"{456:@y}");
}

var msg = processor.Dequeue();
((string)msg["Message"]).Should().Be("100");
((string)msg["Message"]).Should().Be("456");
((int)msg["scope"]["X"]).Should().Be(123);
((int)msg["attributes"]["y"]).Should().Be(456);
}
Expand Down

0 comments on commit 11b41b2

Please sign in to comment.