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 630ab89
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 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
16 changes: 8 additions & 8 deletions tests/ZLogger.MessagePack.Tests/MessagePackEncodedTextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ public class MessagePackEncodedTextTest
[Fact]
public void Encode()
{
var result1 = MessagePackSerializer.Serialize<string>("CategoryName");
MessagePackEncodedText.Encode("CategoryName").Should().BeEquivalentTo(result1);
var fixStrEncoded = MessagePackSerializer.Serialize<string>("CategoryName");
MessagePackEncodedText.Encode("CategoryName").Utf8EncodedValue.ToArray().Should().BeEquivalentTo(fixStrEncoded);

var str8 = new string('a', 512);
var result2 = MessagePackEncodedText.Encode(str8);
MessagePackEncodedText.Encode(str8).Should().BeEquivalentTo(result2);
var str8Encoded = MessagePackSerializer.Serialize(str8);
MessagePackEncodedText.Encode(str8).Utf8EncodedValue.ToArray().Should().BeEquivalentTo(str8Encoded);

var str16 = new string('a', 65535);
var result3 = MessagePackEncodedText.Encode(str16);
MessagePackEncodedText.Encode(str16).Should().BeEquivalentTo(result3);
var str16Encoded = MessagePackSerializer.Serialize(str16);
MessagePackEncodedText.Encode(str16).Utf8EncodedValue.ToArray().Should().BeEquivalentTo(str16Encoded);

var str32 = new string('a', 65536);
var result4 = MessagePackEncodedText.Encode(str32);
MessagePackEncodedText.Encode(str32).Should().BeEquivalentTo(result4);
var str32Encoded = MessagePackSerializer.Serialize(str32);
MessagePackEncodedText.Encode(str32).Utf8EncodedValue.ToArray().Should().BeEquivalentTo(str32Encoded);
}
}

0 comments on commit 630ab89

Please sign in to comment.