diff --git a/src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs b/src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs index 5e28f6d6..d732ba71 100644 --- a/src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs +++ b/src/ZLogger.MessagePack/MessagePackZLoggerFormatter.cs @@ -17,7 +17,7 @@ public static ZLoggerOptions UseMessagePackFormatter(this ZLoggerOptions options }); } } - + public class MessagePackZLoggerFormatter : IZLoggerFormatter { // "CategoryName" @@ -118,7 +118,7 @@ public void FormatLogEntry(IBufferWriter writer, TEntry entry) whe messagePackWriter.WriteRaw(ExceptionKey); WriteException(ref messagePackWriter, ex); } - + for (var i = 0; i < entry.ParameterCount; i++) { if (entry.IsSupportUtf8ParameterKey) @@ -130,197 +130,8 @@ public void FormatLogEntry(IBufferWriter writer, TEntry entry) whe { WriteKeyName(ref messagePackWriter, entry, i); } - - var valueType = entry.GetParameterType(i); - if (valueType == typeof(string)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(bool)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(bool?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(byte)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(byte?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(Int16)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(Int16?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(UInt16)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(UInt16?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(Int32)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(Int32?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(UInt32)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(UInt32?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(Int64)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(Int64?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(UInt64)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(UInt16?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(float)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(float?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(double)) - { - messagePackWriter.Write(entry.GetParameterValue(i)); - } - else if (valueType == typeof(double?)) - { - var nullableValue = entry.GetParameterValue(i); - if (nullableValue.HasValue) - { - messagePackWriter.Write(nullableValue.Value); - } - else - { - messagePackWriter.WriteNil(); - } - } - else if (valueType == typeof(DateTime)) - { - var value = entry.GetParameterValue(i); - MessagePackSerializer.Serialize(valueType, ref messagePackWriter, value, MessagePackSerializerOptions); - } - else if (valueType == typeof(DateTime?)) - { - var value = entry.GetParameterValue(i); - MessagePackSerializer.Serialize(valueType, ref messagePackWriter, value, MessagePackSerializerOptions); - } - else if (valueType == typeof(DateTimeOffset)) - { - var value = entry.GetParameterValue(i); - MessagePackSerializer.Serialize(valueType, ref messagePackWriter, value, MessagePackSerializerOptions); - } - else if (valueType == typeof(DateTimeOffset?)) - { - var value = entry.GetParameterValue(i); - MessagePackSerializer.Serialize(valueType, ref messagePackWriter, value, MessagePackSerializerOptions); - } - else // TODO: GUID, TimeSpan - { - var boxedValue = entry.GetParameterValue(i); - MessagePackSerializer.Serialize(valueType, ref messagePackWriter, boxedValue, MessagePackSerializerOptions); - } + + WriteParameterValue(ref messagePackWriter, entry, entry.GetParameterType(i), i); } if (entry.ScopeState != null) @@ -420,6 +231,224 @@ static void WriteException(ref MessagePackWriter messagePackWriter, Exception? e WriteException(ref messagePackWriter, ex.InnerException); } + void WriteParameterValue(ref MessagePackWriter messagePackWriter, TEntry entry, Type type, int index) + where TEntry : IZLoggerEntry + { + var code = Type.GetTypeCode(type); + switch (code) + { + case TypeCode.Boolean: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Char: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.SByte: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Byte: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Int16: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.UInt16: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Int32: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.UInt32: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Int64: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.UInt64: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Single: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.Double: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + case TypeCode.DateTime: + return; + case TypeCode.String: + messagePackWriter.Write(entry.GetParameterValue(index)); + return; + } + + if (type.IsValueType) + { + if (type == typeof(DateTime)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (type == typeof(DateTimeOffset)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (type == typeof(TimeSpan)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (type == typeof(TimeOnly)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (type == typeof(DateOnly)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (type == typeof(Guid)) + { + var value = entry.GetParameterValue(index); + MessagePackSerializer.Serialize(type, ref messagePackWriter, value, MessagePackSerializerOptions); + } + else if (Nullable.GetUnderlyingType(type) is { } underlyingType) + { + code = Type.GetTypeCode(underlyingType); + switch (code) + { + case TypeCode.Boolean: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Char: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.SByte: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Byte: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Int16: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.UInt16: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Int32: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.UInt32: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Int64: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.UInt64: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Single: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.Double: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + case TypeCode.DateTime: + { + var nullableValue = entry.GetParameterValue(index); + if (nullableValue.HasValue) + messagePackWriter.Write(nullableValue.Value); + else + messagePackWriter.WriteNil(); + return; + } + } + } + } + + var boxedValue = entry.GetParameterValue(index); + if (boxedValue == null) + { + messagePackWriter.WriteNil(); + } + else + { + MessagePackSerializer.Serialize(type, ref messagePackWriter, boxedValue, MessagePackSerializerOptions); + } + } + static byte[] EncodedLogLevel(LogLevel logLevel) { switch (logLevel) diff --git a/src/ZLogger.MessagePack/ZLogger.MessagePack.csproj b/src/ZLogger.MessagePack/ZLogger.MessagePack.csproj index 2119a005..bf5c10ba 100644 --- a/src/ZLogger.MessagePack/ZLogger.MessagePack.csproj +++ b/src/ZLogger.MessagePack/ZLogger.MessagePack.csproj @@ -10,7 +10,7 @@ - + diff --git a/tests/ZLogger.MessagePack.Tests/FormatterTest.cs b/tests/ZLogger.MessagePack.Tests/FormatterTest.cs index 707f784a..4425adff 100644 --- a/tests/ZLogger.MessagePack.Tests/FormatterTest.cs +++ b/tests/ZLogger.MessagePack.Tests/FormatterTest.cs @@ -102,7 +102,7 @@ public void WithParameters() var now = DateTime.UtcNow; var payload = new TestPayload { X = 999 }; var x = 100; - var y = 200; + int? y = null; logger.ZLogInformation(new EventId(1, "HOGE"), $"UMU {payload} {x} {y}"); var msgpack = processor.Dequeue(); @@ -111,9 +111,9 @@ public void WithParameters() ((int)msgpack["EventId"]).Should().Be(1); ((string)msgpack["EventIdName"]).Should().Be("HOGE"); ((DateTime)msgpack["Timestamp"]).Should().BeOnOrAfter(now); + ((int?)msgpack["x"]).Should().Be(100); + ((int?)msgpack["y"]).Should().Be(null); ((int)msgpack["payload"]["x"]).Should().Be(999); - ((int)msgpack["x"]).Should().Be(100); - ((int)msgpack["y"]).Should().Be(200); ((bool)msgpack.ContainsKey("Exception")).Should().BeFalse(); } @@ -149,4 +149,4 @@ public void LowercaseMutator() ((int)msgpack["fOo"]).Should().Be(200); } } -} \ No newline at end of file +} diff --git a/tests/ZLogger.Tests/EnumDictionaryTest.cs b/tests/ZLogger.Tests/EnumDictionaryTest.cs index 00f309d6..3885c4a0 100644 --- a/tests/ZLogger.Tests/EnumDictionaryTest.cs +++ b/tests/ZLogger.Tests/EnumDictionaryTest.cs @@ -1,14 +1,10 @@ using FluentAssertions; using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; -using System.Threading.Tasks; -using Xunit; using ZLogger.Internal; namespace ZLogger.Tests