diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs index ef6b91a0..f24011ce 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, alignment, format); + } } diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt b/src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt index 10d51b9a..ff121695 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + this.innerHandler.AppendFormatted(namedValue, alignment, format); + } } <# } #> \ No newline at end of file diff --git a/src/ZLogger/ZLoggerInterpolatedStringHandler.cs b/src/ZLogger/ZLoggerInterpolatedStringHandler.cs index 36050d0b..0dfd26bb 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((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null) + { + AppendFormatted(namedValue.Item2, alignment, format, namedValue.Item1); + } + internal InterpolatedStringLogState GetStateAndClear() { // MessageSequence is immutable diff --git a/tests/ZLogger.Tests/NamedParamTest.cs b/tests/ZLogger.Tests/NamedParamTest.cs new file mode 100644 index 00000000..b3f75a18 --- /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($"{("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; + + 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)); + } + } +}