Skip to content

Commit c7bc3ae

Browse files
author
Vicenc Garcia
committed
fix: remove global properties if repeated
1 parent 825bee6 commit c7bc3ae

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

build/dependencies.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
</PropertyGroup>
66

77
<PropertyGroup Label="MiddyNet Versions">
8-
<middynet>1.3.0$(VersionSuffix)</middynet>
8+
<middynet>1.3.1$(VersionSuffix)</middynet>
99
</PropertyGroup>
1010
</Project>

src/Voxel.MiddyNet/MiddyLogger.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ private void InternalLog(LogMessage logMessage)
6969

7070
private void AddLambdaContextProperties()
7171
{
72-
globalProperties.Add(new LogProperty("AwsRequestId", lambdaContext.AwsRequestId));
73-
globalProperties.Add(new LogProperty("FunctionName", lambdaContext.FunctionName));
74-
globalProperties.Add(new LogProperty("FunctionVersion", lambdaContext.FunctionVersion));
75-
globalProperties.Add(new LogProperty("MemoryLimitInMB", lambdaContext.MemoryLimitInMB));
72+
AddOrReplaceProperty("AwsRequestId", lambdaContext.AwsRequestId);
73+
AddOrReplaceProperty("FunctionName", lambdaContext.FunctionName);
74+
AddOrReplaceProperty("FunctionVersion", lambdaContext.FunctionVersion);
75+
AddOrReplaceProperty("MemoryLimitInMB", lambdaContext.MemoryLimitInMB);
76+
}
77+
78+
private void AddOrReplaceProperty(string name, object value)
79+
{
80+
var index = globalProperties.FindIndex(p => p.Key == name);
81+
if (index != -1) globalProperties.RemoveAt(index);
82+
83+
globalProperties.Add(new LogProperty(name, value));
7684
}
7785
}
7886

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Message": "hello world",
3+
"Level": "Debug",
4+
"AwsRequestId": "12345",
5+
"FunctionName": "FunctionName",
6+
"FunctionVersion": "1.0",
7+
"MemoryLimitInMB": 1024
8+
}{
9+
"Message": "hello world",
10+
"Level": "Debug",
11+
"AwsRequestId": "12345",
12+
"FunctionName": "FunctionName",
13+
"FunctionVersion": "1.0",
14+
"MemoryLimitInMB": 1024
15+
}

test/Voxel.MiddyNet.Tests/MiddyLoggerShould.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MiddyLoggerShould
2121

2222
public MiddyLoggerShould()
2323
{
24-
lambdaLogger.Log(Arg.Do<string>(a => receivedLog = a));
24+
lambdaLogger.Log(Arg.Do<string>(a => receivedLog += a));
2525
lambdaContext.AwsRequestId.Returns(AwsRequestId);
2626
lambdaContext.FunctionName.Returns(FunctionName);
2727
lambdaContext.FunctionVersion.Returns(FunctionVersion);
@@ -80,10 +80,25 @@ public void LogGlobalPropertiesAndExtraProperties()
8080
Approvals.Verify(receivedLog);
8181
}
8282

83+
[Fact]
84+
public void LogTwiceWithNoErrors()
85+
{
86+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
87+
logger.Log(LogLevel.Debug, "hello world");
88+
logger.Log(LogLevel.Debug, "hello world");
89+
90+
Approvals.Verify(receivedLog);
91+
}
92+
8393
internal class ClassToLog
8494
{
8595
public string Property1 { get; set; }
8696
public string Property2 { get; set; }
8797
}
98+
99+
~MiddyLoggerShould()
100+
{
101+
receivedLog = string.Empty;
102+
}
88103
}
89104
}

0 commit comments

Comments
 (0)