Skip to content

Commit

Permalink
Add ZLogger.Param(keyName, value)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Oct 26, 2023
1 parent f00cde0 commit f4481b0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/ZLogger/Formatters/SystemTextJsonZLoggerFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Buffers;
using System.Text.Encodings.Web;
using System.Text.Json;
Expand Down
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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
this.innerHandler.AppendFormatted(value, 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>(ZLoggerNamedParam<T> value, int alignment = 0, string? format = null, string? _ = null)
{
AppendFormatted(value.Value, alignment, format, value.Name);
}

internal InterpolatedStringLogState GetStateAndClear()
{
// MessageSequence is immutable
Expand Down
20 changes: 20 additions & 0 deletions src/ZLogger/ZLoggerParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ZLogger
{
// The helper class used directly with logger
public static partial class ZLogger
{
public static ZLoggerNamedParam<T> Param<T>(string name, T value) => new(name, value);
}

public readonly struct ZLoggerNamedParam<T>
{
public readonly string Name;
public readonly T Value;

public ZLoggerNamedParam(string name, T value)
{
Name = name;
Value = value;
}
}
}
1 change: 0 additions & 1 deletion tests/ZLogger.Tests/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,5 @@ public void StructuredLoggingOptions()
doc.GetProperty("Hash").GetString().Should().Be(sourceCodeHash);
doc.GetProperty("LogLevel").GetString().Should().Be("Debug");
}

}
}
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($"{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));
}
}
}

0 comments on commit f4481b0

Please sign in to comment.