Skip to content

Commit 456abe8

Browse files
committed
apply property name policy to property name
1 parent 78c4515 commit 456abe8

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/ExceptionConverter.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, Js
5353
/// </summary>
5454
/// <param name="writer">The unicode JsonWriter.</param>
5555
/// <param name="value">The exception instance.</param>
56-
/// <param name="options">The Json serializer options.</param>
56+
/// <param name="options">The JsonSerializer options.</param>
5757
public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
5858
{
5959
var exceptionType = value.GetType();
@@ -69,28 +69,41 @@ public override void Write(Utf8JsonWriter writer, Exception value, JsonSerialize
6969
return;
7070

7171
writer.WriteStartObject();
72-
writer.WriteString("Type", exceptionType.FullName);
73-
72+
writer.WriteString(ApplyPropertyNamingPolicy("Type", options), exceptionType.FullName);
73+
7474
foreach (var prop in props)
7575
{
7676
switch (prop.Value)
7777
{
7878
case IntPtr intPtr:
79-
writer.WriteNumber(prop.Name, intPtr.ToInt64());
79+
writer.WriteNumber(ApplyPropertyNamingPolicy(prop.Name, options), intPtr.ToInt64());
8080
break;
8181
case UIntPtr uIntPtr:
82-
writer.WriteNumber(prop.Name, uIntPtr.ToUInt64());
82+
writer.WriteNumber(ApplyPropertyNamingPolicy(prop.Name, options), uIntPtr.ToUInt64());
8383
break;
8484
case Type propType:
85-
writer.WriteString(prop.Name, propType.FullName);
85+
writer.WriteString(ApplyPropertyNamingPolicy(prop.Name, options), propType.FullName);
8686
break;
8787
default:
88-
writer.WritePropertyName(prop.Name);
88+
writer.WritePropertyName(ApplyPropertyNamingPolicy(prop.Name, options));
8989
JsonSerializer.Serialize(writer, prop.Value, options);
9090
break;
9191
}
9292
}
9393

9494
writer.WriteEndObject();
9595
}
96+
97+
/// <summary>
98+
/// Applying the property naming policy to property name
99+
/// </summary>
100+
/// <param name="propertyName">The name of the property</param>
101+
/// <param name="options">The JsonSerializer options.</param>
102+
/// <returns></returns>
103+
private static string ApplyPropertyNamingPolicy(string propertyName, JsonSerializerOptions options)
104+
{
105+
return !string.IsNullOrWhiteSpace(propertyName) && options?.PropertyNamingPolicy is not null
106+
? options.PropertyNamingPolicy.ConvertName(propertyName)
107+
: propertyName;
108+
}
96109
}

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public void Log_WhenException_LogsExceptionDetails()
10471047
v.LogLine(
10481048
It.Is<string>
10491049
(s =>
1050-
s.Contains("\"exception\":{\"Type\":\"" + error.GetType().FullName + "\",\"Message\":\"" + error.Message + "\"")
1050+
s.Contains("\"exception\":{\"type\":\"" + error.GetType().FullName + "\",\"message\":\"" + error.Message + "\"")
10511051
)
10521052
), Times.Once);
10531053
}
@@ -1090,7 +1090,7 @@ public void Log_WhenNestedException_LogsExceptionDetails()
10901090
v.LogLine(
10911091
It.Is<string>
10921092
(s =>
1093-
s.Contains("\"error\":{\"Type\":\"" + error.GetType().FullName + "\",\"Message\":\"" + error.Message + "\"")
1093+
s.Contains("\"error\":{\"type\":\"" + error.GetType().FullName + "\",\"message\":\"" + error.Message + "\"")
10941094
)
10951095
), Times.Once);
10961096
}

0 commit comments

Comments
 (0)