From f4481b0b547dc47ba6859b33d1893d6489ed90a8 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 20 Oct 2023 14:25:39 +0900 Subject: [PATCH] Add ZLogger.Param(keyName, value) --- .../SystemTextJsonZLoggerFormatter.cs | 1 - ...oggerInterpolatedStringHandler.LogLevel.cs | 30 ++++++++++++++++ ...oggerInterpolatedStringHandler.LogLevel.tt | 5 +++ .../ZLoggerInterpolatedStringHandler.cs | 5 +++ src/ZLogger/ZLoggerParam.cs | 20 +++++++++++ tests/ZLogger.Tests/MessageTest.cs | 1 - tests/ZLogger.Tests/NamedParamTest.cs | 36 +++++++++++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/ZLogger/ZLoggerParam.cs create mode 100644 tests/ZLogger.Tests/NamedParamTest.cs diff --git a/src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs b/src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs index 8f37e9da..f30799d9 100644 --- a/src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs +++ b/src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs @@ -1,4 +1,3 @@ -using System; using System.Buffers; using System.Text.Encodings.Web; using System.Text.Json; diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs index ef6b91a0..3fc498f5 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs @@ -28,6 +28,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } [InterpolatedStringHandler] @@ -54,6 +59,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } [InterpolatedStringHandler] @@ -80,6 +90,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } [InterpolatedStringHandler] @@ -106,6 +121,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } [InterpolatedStringHandler] @@ -132,6 +152,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } [InterpolatedStringHandler] @@ -158,5 +183,10 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt index 10d51b9a..39413fa8 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt @@ -38,6 +38,11 @@ public ref struct ZLogger<#= logLevel #>InterpolatedStringHandler { this.innerHandler.AppendFormatted(value, alignment, format, argumentName); } + + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(value, alignment, format); + } } <# } #> \ No newline at end of file diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.cs index e2185d11..e444cf9c 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.cs +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.cs @@ -109,6 +109,11 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for } } + public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + { + AppendFormatted(value.Value, alignment, format, value.Name); + } + internal InterpolatedStringLogState GetStateAndClear() { // MessageSequence is immutable diff --git a/src/ZLogger/ZLoggerParam.cs b/src/ZLogger/ZLoggerParam.cs new file mode 100644 index 00000000..f95c24b4 --- /dev/null +++ b/src/ZLogger/ZLoggerParam.cs @@ -0,0 +1,20 @@ +namespace ZLogger +{ + // The helper class used directly with logger + public static partial class ZLogger + { + public static ZLoggerNamedParam Param(string name, T value) => new(name, value); + } + + public readonly struct ZLoggerNamedParam + { + public readonly string Name; + public readonly T Value; + + public ZLoggerNamedParam(string name, T value) + { + Name = name; + Value = value; + } + } +} diff --git a/tests/ZLogger.Tests/MessageTest.cs b/tests/ZLogger.Tests/MessageTest.cs index 9ff9d9ab..78e56e76 100644 --- a/tests/ZLogger.Tests/MessageTest.cs +++ b/tests/ZLogger.Tests/MessageTest.cs @@ -163,6 +163,5 @@ public void StructuredLoggingOptions() doc.GetProperty("Hash").GetString().Should().Be(sourceCodeHash); doc.GetProperty("LogLevel").GetString().Should().Be("Debug"); } - } } diff --git a/tests/ZLogger.Tests/NamedParamTest.cs b/tests/ZLogger.Tests/NamedParamTest.cs new file mode 100644 index 00000000..6e333689 --- /dev/null +++ b/tests/ZLogger.Tests/NamedParamTest.cs @@ -0,0 +1,36 @@ +using System; +using System.Text.Json; +using FluentAssertions; +using Microsoft.Extensions.Logging; +using ZLogger.Formatters; + +namespace ZLogger.Tests +{ + public class NamedParamTest + { + [Fact] + public void StructuredLoggingOptions() + { + var options = new ZLoggerOptions(); + options.UseJsonFormatter(); + + var processor = new TestProcessor(options); + using var loggerFactory = LoggerFactory.Create(x => + { + x.SetMinimumLevel(LogLevel.Debug); + x.AddZLoggerLogProcessor(processor); + }); + var logger = loggerFactory.CreateLogger("test"); + + logger.ZLogInformation($"{ZLogger.Param("TAKO", 100)} {ZLogger.Param("YAKI", 200):D5} {ZLogger.Param("T", new DateTime(2023, 12, 31)),15:yyyy-MM-dd}"); + + var json = processor.EntryMessages.Dequeue(); + var doc = JsonDocument.Parse(json).RootElement; + + doc.GetProperty("Message").GetString().Should().Be("100 00200 2023-12-31"); + doc.GetProperty("TAKO").GetInt32().Should().Be(100); + doc.GetProperty("YAKI").GetInt32().Should().Be(200); + doc.GetProperty("T").GetDateTime().Should().Be(new DateTime(2023, 12, 31)); + } + } +}