Skip to content

Commit

Permalink
Merge pull request #92 from Cysharp/hadashiA/named-param
Browse files Browse the repository at this point in the history
Add helper method to name parameter like  `$”{(keyName, value)}`
  • Loading branch information
neuecc authored Nov 9, 2023
2 parents 394c449 + 1ff161e commit 33f14fc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

[InterpolatedStringHandler]
Expand All @@ -54,6 +59,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

[InterpolatedStringHandler]
Expand All @@ -80,6 +90,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

[InterpolatedStringHandler]
Expand All @@ -106,6 +121,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

[InterpolatedStringHandler]
Expand All @@ -132,6 +152,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

[InterpolatedStringHandler]
Expand All @@ -158,5 +183,10 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

5 changes: 5 additions & 0 deletions src/ZLogger/ZLoggerInterpolatedStringHandler.LogLevel.tt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public ref struct ZLogger<#= logLevel #>InterpolatedStringHandler
{
this.innerHandler.AppendFormatted<T>(value, alignment, format, argumentName);
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(namedValue, alignment, format);
}
}

<# } #>
5 changes: 5 additions & 0 deletions src/ZLogger/ZLoggerInterpolatedStringHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
}
}

public void AppendFormatted<T>((string, T) namedValue, int alignment = 0, string? format = null, string? _ = null)
{
AppendFormatted(namedValue.Item2, alignment, format, namedValue.Item1);
}

internal InterpolatedStringLogState GetStateAndClear()
{
// MessageSequence is immutable
Expand Down
36 changes: 36 additions & 0 deletions tests/ZLogger.Tests/NamedParamTest.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 33f14fc

Please sign in to comment.