Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 7, 2023
1 parent ece37c2 commit 1203b62
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 86 deletions.
10 changes: 5 additions & 5 deletions src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Numerics;
using MessagePack;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -100,12 +101,9 @@ public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) whe
if (entry.LogInfo.ScopeState != null)
{
var scopeProperties = entry.LogInfo.ScopeState.Properties;
for (var i = 0; i < scopeProperties.Length; i++)
foreach (var t in scopeProperties)
{
if (scopeProperties[i].Key != "{OriginalFormat}")
{
scopePropCount++;
}
if (t.Key != "{OriginalFormat}") scopePropCount++;
}
}

Expand Down Expand Up @@ -173,6 +171,7 @@ public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) whe
// nested
if (PropertyNames.ScopeKeyValues != null)
{
messagePackWriter.WriteRaw(PropertyNames.ScopeKeyValues.Value.Utf8EncodedValue);
messagePackWriter.WriteMapHeader(scopePropCount);
}

Expand Down Expand Up @@ -201,6 +200,7 @@ public void FormatLogEntry<TEntry>(IBufferWriter<byte> writer, TEntry entry) whe
// nested
if (PropertyNames.ParameterKeyValues != null)
{
messagePackWriter.WriteRaw(PropertyNames.ParameterKeyValues.Value.Utf8EncodedValue);
messagePackWriter.WriteMapHeader(entry.ParameterCount);
}
for (var i = 0; i < entry.ParameterCount; i++)
Expand Down
25 changes: 16 additions & 9 deletions tests/ZLogger.MessagePack.Tests/FormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,17 @@ public void CustomPropertyNames()
}

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

processor = new TestProcessor(formatter);

var loggerFactory = LoggerFactory.Create(x =>
Expand All @@ -258,13 +261,17 @@ public void Nested()
});
});
logger = loggerFactory.CreateLogger("test");


logger.ZLogInformation($"{100:@x}");


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

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

[Fact]
Expand Down
143 changes: 71 additions & 72 deletions tests/ZLogger.MessagePack.Tests/ScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,97 @@
using FluentAssertions;
using Xunit;

namespace ZLogger.MessagePack.Tests
namespace ZLogger.MessagePack.Tests;

public class ScopeTest
{
public class ScopeTest
TestProcessor processor;
ILogger logger;

public ScopeTest()
{
TestProcessor processor;
ILogger logger;
processor = new TestProcessor(new MessagePackZLoggerFormatter());

public ScopeTest()
var loggerFactory = LoggerFactory.Create(x =>
{
processor = new TestProcessor(new MessagePackZLoggerFormatter());

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

[Fact]
public void BeginScope_FormattedLogValuesToMessagePack()
[Fact]
public void BeginScope_FormattedLogValuesToMessagePack()
{
using (logger.BeginScope("({X}, {Y})", 111, null))
{
using (logger.BeginScope("({X}, {Y})", 111, null))
{
var a = 333;
var b = 444;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}
var a = 333;
var b = 444;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}

var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar333 NanoNano444");
((int)msgpack["X"]).Should().Be(111);
((string?)msgpack["Y"]).Should().Be(null);
var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar333 NanoNano444");
((int)msgpack["X"]).Should().Be(111);
((string?)msgpack["Y"]).Should().Be(null);

}
}

[Fact]
public void BeginScope_KeyValuePairToJson()
[Fact]
public void BeginScope_KeyValuePair()
{
using (logger.BeginScope(new KeyValuePair<string, object?>("Hoge", "AAA")))
{
using (logger.BeginScope(new KeyValuePair<string, object?>("Hoge", "AAA")))
{
var a = 100;
var b = 200;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}

var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar100 NanoNano200");
((string)msgpack["Hoge"]).Should().Be("AAA");
var a = 100;
var b = 200;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}

[Fact]
public void BeginScope_AnyScopeValueToJson()
{
using (logger.BeginScope(new TestPayload { X = 999 }))
{
var a = 100;
var b = 200;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}
var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar100 NanoNano200");
((string)msgpack["Hoge"]).Should().Be("AAA");
}

var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar100 NanoNano200");
((int)msgpack["Scope"]["x"]).Should().Be(999);
[Fact]
public void BeginScope_AnyScopeValue()
{
using (logger.BeginScope(new TestPayload { X = 999 }))
{
var a = 100;
var b = 200;
logger.ZLogInformation($"FooBar{a} NanoNano{b}");
}

[Fact]
public void BeginScope_NestedToJson()
var msgpack = processor.Dequeue();
((string)msgpack["Message"]).Should().Be("FooBar100 NanoNano200");
((int)msgpack["Scope"]["x"]).Should().Be(999);
}

[Fact]
public void BeginScope_Nested()
{
using (logger.BeginScope("A={A}", 100))
{
using (logger.BeginScope("A={A}", 100))
{
logger.ZLogInformation($"Message 1");
logger.ZLogInformation($"Message 1");

using (logger.BeginScope("B={B}", 200))
{
logger.ZLogInformation($"Message 2");
}
using (logger.BeginScope("B={B}", 200))
{
logger.ZLogInformation($"Message 2");
}
}

var msgpack1 = processor.Dequeue();
var msgpack2 = processor.Dequeue();
((string)msgpack1["Message"]).Should().Be("Message 1");
((int)msgpack1["A"]).Should().Be(100);
((bool)msgpack1.ContainsKey("B")).Should().BeFalse();
var msgpack1 = processor.Dequeue();
var msgpack2 = processor.Dequeue();
((string)msgpack1["Message"]).Should().Be("Message 1");
((int)msgpack1["A"]).Should().Be(100);
((bool)msgpack1.ContainsKey("B")).Should().BeFalse();

((string)msgpack2["Message"]).Should().Be("Message 2");
((int)msgpack2["A"]).Should().Be(100);
((int)msgpack2["B"]).Should().Be(200);
}
((string)msgpack2["Message"]).Should().Be("Message 2");
((int)msgpack2["A"]).Should().Be(100);
((int)msgpack2["B"]).Should().Be(200);
}
}
}

0 comments on commit 1203b62

Please sign in to comment.