-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/hjgraca/powertools-lambd…
…a-dotnet into fix(tracing)-aot-void-task-and-serialization
- Loading branch information
Showing
9 changed files
with
347 additions
and
26 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
19 changes: 19 additions & 0 deletions
19
libraries/src/AWS.Lambda.Powertools.Logging/Serializers/LogLevelJsonConverter.cs
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,19 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace AWS.Lambda.Powertools.Logging.Serializers; | ||
|
||
internal class LogLevelJsonConverter : JsonConverter<LogLevel> | ||
{ | ||
public override LogLevel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return Enum.TryParse<LogLevel>(reader.GetString(),true, out var val) ? val : default; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, LogLevel value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} |
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
141 changes: 141 additions & 0 deletions
141
libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Utilities/LogLevelJsonConverterTests.cs
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,141 @@ | ||
using System.Text.Json; | ||
using AWS.Lambda.Powertools.Logging.Serializers; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace AWS.Lambda.Powertools.Logging.Tests.Utilities; | ||
|
||
public class LogLevelJsonConverterTests | ||
{ | ||
private readonly LogLevelJsonConverter _converter; | ||
private readonly JsonSerializerOptions _options; | ||
|
||
public LogLevelJsonConverterTests() | ||
{ | ||
_converter = new LogLevelJsonConverter(); | ||
_options = new JsonSerializerOptions | ||
{ | ||
Converters = { _converter } | ||
}; | ||
} | ||
|
||
[Theory] | ||
[InlineData("Information", LogLevel.Information)] | ||
[InlineData("Error", LogLevel.Error)] | ||
[InlineData("Warning", LogLevel.Warning)] | ||
[InlineData("Debug", LogLevel.Debug)] | ||
[InlineData("Trace", LogLevel.Trace)] | ||
[InlineData("Critical", LogLevel.Critical)] | ||
[InlineData("None", LogLevel.None)] | ||
public void Read_ValidLogLevel_ReturnsCorrectEnum(string input, LogLevel expected) | ||
{ | ||
// Arrange | ||
var json = $"\"{input}\""; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<LogLevel>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[InlineData("information", LogLevel.Information)] | ||
[InlineData("ERROR", LogLevel.Error)] | ||
[InlineData("Warning", LogLevel.Warning)] | ||
[InlineData("deBUG", LogLevel.Debug)] | ||
public void Read_CaseInsensitive_ReturnsCorrectEnum(string input, LogLevel expected) | ||
{ | ||
// Arrange | ||
var json = $"\"{input}\""; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<LogLevel>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("InvalidLevel")] | ||
[InlineData("NotALevel")] | ||
public void Read_InvalidLogLevel_ReturnsDefault(string input) | ||
{ | ||
// Arrange | ||
var json = $"\"{input}\""; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<LogLevel>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(default(LogLevel), result); | ||
} | ||
|
||
[Fact] | ||
public void Read_NullValue_ReturnsDefault() | ||
{ | ||
// Arrange | ||
var json = "null"; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<LogLevel>(json, _options); | ||
|
||
// Assert | ||
Assert.Equal(default(LogLevel), result); | ||
} | ||
|
||
[Theory] | ||
[InlineData(LogLevel.Information, "Information")] | ||
[InlineData(LogLevel.Error, "Error")] | ||
[InlineData(LogLevel.Warning, "Warning")] | ||
[InlineData(LogLevel.Debug, "Debug")] | ||
[InlineData(LogLevel.Trace, "Trace")] | ||
[InlineData(LogLevel.Critical, "Critical")] | ||
[InlineData(LogLevel.None, "None")] | ||
public void Write_ValidLogLevel_WritesCorrectString(LogLevel input, string expected) | ||
{ | ||
// Act | ||
var result = JsonSerializer.Serialize(input, _options); | ||
|
||
// Assert | ||
Assert.Equal($"\"{expected}\"", result); | ||
} | ||
|
||
[Fact] | ||
public void Write_DefaultLogLevel_WritesCorrectString() | ||
{ | ||
// Arrange | ||
var input = default(LogLevel); | ||
|
||
// Act | ||
var result = JsonSerializer.Serialize(input, _options); | ||
|
||
// Assert | ||
Assert.Equal($"\"{input}\"", result); | ||
} | ||
|
||
[Fact] | ||
public void Converter_CanConvert_LogLevelType() | ||
{ | ||
// Act | ||
var canConvert = _converter.CanConvert(typeof(LogLevel)); | ||
|
||
// Assert | ||
Assert.True(canConvert); | ||
} | ||
|
||
[Fact] | ||
public void SerializeAndDeserialize_RoundTrip_MaintainsValue() | ||
{ | ||
// Arrange | ||
var originalValue = LogLevel.Information; | ||
|
||
// Act | ||
var serialized = JsonSerializer.Serialize(originalValue, _options); | ||
var deserialized = JsonSerializer.Deserialize<LogLevel>(serialized, _options); | ||
|
||
// Assert | ||
Assert.Equal(originalValue, deserialized); | ||
} | ||
} |
Oops, something went wrong.