-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |