From 1aefce8fab9c9c8f5630146fd304c4102f250b9f Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 20 Oct 2023 14:25:39 +0900 Subject: [PATCH 1/2] Add ZLogger.Param(keyName, value) --- ...oggerInterpolatedStringHandler.LogLevel.cs | 30 ++++++++++++++++ ...oggerInterpolatedStringHandler.LogLevel.tt | 5 +++ .../ZLoggerInterpolatedStringHandler.cs | 5 +++ src/ZLogger/ZLoggerParam.cs | 20 +++++++++++ tests/ZLogger.Tests/NamedParamTest.cs | 36 +++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 src/ZLogger/ZLoggerParam.cs create mode 100644 tests/ZLogger.Tests/NamedParamTest.cs 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 36050d0b..c48fe713 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/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)); + } + } +} From 1ff161ea69e720ce7b3f46f4f8b16e452ab06d95 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 20 Oct 2023 20:29:55 +0900 Subject: [PATCH 2/2] Use (string, T) instead of ZLogger.Param --- ...oggerInterpolatedStringHandler.LogLevel.cs | 24 +++++++++---------- ...oggerInterpolatedStringHandler.LogLevel.tt | 4 ++-- .../ZLoggerInterpolatedStringHandler.cs | 4 ++-- src/ZLogger/ZLoggerParam.cs | 20 ---------------- tests/ZLogger.Tests/NamedParamTest.cs | 2 +- 5 files changed, 17 insertions(+), 37 deletions(-) delete mode 100644 src/ZLogger/ZLoggerParam.cs diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs index 3fc498f5..f24011ce 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs @@ -29,9 +29,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } @@ -60,9 +60,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } @@ -91,9 +91,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } @@ -122,9 +122,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } @@ -153,9 +153,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } @@ -184,9 +184,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt index 39413fa8..ff121695 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt @@ -39,9 +39,9 @@ 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) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - this.innerHandler.AppendFormatted(value, alignment, format); + this.innerHandler.AppendFormatted(namedValue, alignment, format); } } diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.cs index c48fe713..0dfd26bb 100644 --- a/src/ZLogger/ZLoggerInterpolatedStringHandler.cs +++ b/src/ZLogger/ZLoggerInterpolatedStringHandler.cs @@ -109,9 +109,9 @@ public void AppendFormatted(Nullable value, int alignment = 0, string? for } } - public void AppendFormatted(ZLoggerNamedParam value, int alignment = 0, string? format = null, string? _ = null) + public void AppendFormatted((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) { - AppendFormatted(value.Value, alignment, format, value.Name); + AppendFormatted(namedValue.Item2, alignment, format, namedValue.Item1); } internal InterpolatedStringLogState GetStateAndClear() diff --git a/src/ZLogger/ZLoggerParam.cs b/src/ZLogger/ZLoggerParam.cs deleted file mode 100644 index f95c24b4..00000000 --- a/src/ZLogger/ZLoggerParam.cs +++ /dev/null @@ -1,20 +0,0 @@ -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/NamedParamTest.cs b/tests/ZLogger.Tests/NamedParamTest.cs index 6e333689..b3f75a18 100644 --- a/tests/ZLogger.Tests/NamedParamTest.cs +++ b/tests/ZLogger.Tests/NamedParamTest.cs @@ -22,7 +22,7 @@ public void StructuredLoggingOptions() }); 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}"); + logger.ZLogInformation($"{("TAKO", 100)} {("YAKI", 200):D5} {("T", new DateTime(2023, 12, 31)),15:yyyy-MM-dd}"); var json = processor.EntryMessages.Dequeue(); var doc = JsonDocument.Parse(json).RootElement;