Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method to name parameter like $”{(keyName, value)} #92

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
}
Loading