Skip to content

Commit 009a1ce

Browse files
author
Vicenç García Altés
authored
Merge branch 'main' into FixPublishing
2 parents d0b7565 + 29dd43a commit 009a1ce

9 files changed

+57
-12
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.1.2$(VersionSuffix)</middynet>
8+
<middynet>1.2.0$(VersionSuffix)</middynet>
99
</PropertyGroup>
1010
</Project>

src/Voxel.MiddyNet/MiddyLogger.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ namespace Voxel.MiddyNet
99
public class MiddyLogger : IMiddyLogger
1010
{
1111
private readonly ILambdaLogger lambdaLogger;
12+
private readonly ILambdaContext lambdaContext;
1213

1314
private List<LogProperty> globalProperties = new List<LogProperty>();
1415

15-
public MiddyLogger(ILambdaLogger lambdaLogger)
16+
public MiddyLogger(ILambdaLogger lambdaLogger, ILambdaContext lambdaContext)
1617
{
1718
this.lambdaLogger = lambdaLogger;
19+
this.lambdaContext = lambdaContext;
1820
}
1921

2022
public void Log(LogLevel logLevel, string message)
2123
{
24+
AddLambdaContextProperties();
25+
2226
var logMessage = new LogMessage
2327
{
2428
Level = logLevel,
@@ -31,6 +35,8 @@ public void Log(LogLevel logLevel, string message)
3135

3236
public void Log(LogLevel logLevel, string message, params LogProperty[] properties)
3337
{
38+
AddLambdaContextProperties();
39+
3440
var logMessage = new LogMessage
3541
{
3642
Level = logLevel,
@@ -41,6 +47,11 @@ public void Log(LogLevel logLevel, string message, params LogProperty[] properti
4147
InternalLog(logMessage);
4248
}
4349

50+
public void EnrichWith(LogProperty logProperty)
51+
{
52+
globalProperties.Add(logProperty);
53+
}
54+
4455
private void InternalLog(LogMessage logMessage)
4556
{
4657
var options = new JsonSerializerOptions
@@ -56,9 +67,12 @@ private void InternalLog(LogMessage logMessage)
5667
lambdaLogger.Log(jsonString);
5768
}
5869

59-
public void EnrichWith(LogProperty logProperty)
70+
private void AddLambdaContextProperties()
6071
{
61-
globalProperties.Add(logProperty);
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));
6276
}
6377
}
6478

src/Voxel.MiddyNet/MiddyNetContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public MiddyNetContext(ILambdaContext context)
2121
AdditionalContext = new Dictionary<string, object>();
2222
MiddlewareBeforeExceptions = new List<Exception>();
2323
MiddlewareAfterExceptions = new List<Exception>();
24-
LoggerFactory = logger => new MiddyLogger(logger);
24+
LoggerFactory = logger => new MiddyLogger(logger, context);
2525
AttachToLambdaContext(context);
2626
}
2727

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"Message": "hello world",
33
"Level": "Info",
4-
"key": "value"
4+
"key": "value",
5+
"AwsRequestId": "12345",
6+
"FunctionName": "FunctionName",
7+
"FunctionVersion": "1.0",
8+
"MemoryLimitInMB": 1024
59
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"Message": "hello world",
33
"Level": "Info",
4+
"AwsRequestId": "12345",
5+
"FunctionName": "FunctionName",
6+
"FunctionVersion": "1.0",
7+
"MemoryLimitInMB": 1024,
48
"key": "value"
59
}

test/Voxel.MiddyNet.Tests/MiddyLoggerShould.LogExtraPropertiesWithObject.approved.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"Message": "hello world",
33
"Level": "Info",
4+
"AwsRequestId": "12345",
5+
"FunctionName": "FunctionName",
6+
"FunctionVersion": "1.0",
7+
"MemoryLimitInMB": 1024,
48
"key": {
59
"Property1": "The value of property1",
610
"Property2": "The value of property2"

test/Voxel.MiddyNet.Tests/MiddyLoggerShould.LogGlobalPropertiesAndExtraProperties.approved.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
"Message": "hello world",
33
"Level": "Info",
44
"key": "value",
5+
"AwsRequestId": "12345",
6+
"FunctionName": "FunctionName",
7+
"FunctionVersion": "1.0",
8+
"MemoryLimitInMB": 1024,
59
"key2": "value2"
610
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"Message": "hello world",
3-
"Level": "Debug"
3+
"Level": "Debug",
4+
"AwsRequestId": "12345",
5+
"FunctionName": "FunctionName",
6+
"FunctionVersion": "1.0",
7+
"MemoryLimitInMB": 1024
48
}

test/Voxel.MiddyNet.Tests/MiddyLoggerShould.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@ namespace Voxel.MiddyNet.Tests
1010
public class MiddyLoggerShould
1111
{
1212
private readonly ILambdaLogger lambdaLogger = Substitute.For<ILambdaLogger>();
13+
private readonly ILambdaContext lambdaContext = Substitute.For<ILambdaContext>();
14+
private const string AwsRequestId = "12345";
15+
private const string FunctionName = "FunctionName";
16+
private const string FunctionVersion = "1.0";
17+
private const int MemoryLimitInMb = 1024;
18+
1319
private string receivedLog = string.Empty;
20+
1421

1522
public MiddyLoggerShould()
1623
{
1724
lambdaLogger.Log(Arg.Do<string>(a => receivedLog = a));
25+
lambdaContext.AwsRequestId.Returns(AwsRequestId);
26+
lambdaContext.FunctionName.Returns(FunctionName);
27+
lambdaContext.FunctionVersion.Returns(FunctionVersion);
28+
lambdaContext.MemoryLimitInMB.Returns(MemoryLimitInMb);
1829
}
1930

2031
[Fact]
2132
public void LogLevelAndMessage()
2233
{
23-
var logger = new MiddyLogger(lambdaLogger);
34+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
2435
logger.Log(LogLevel.Debug, "hello world");
2536

2637
Approvals.Verify(receivedLog);
@@ -29,7 +40,7 @@ public void LogLevelAndMessage()
2940
[Fact]
3041
public void LogExtraProperties()
3142
{
32-
var logger = new MiddyLogger(lambdaLogger);
43+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
3344
logger.Log(LogLevel.Info, "hello world", new LogProperty("key", "value"));
3445

3546
Approvals.Verify(receivedLog);
@@ -44,7 +55,7 @@ public void LogExtraPropertiesWithObject()
4455
Property2 = "The value of property2"
4556
};
4657

47-
var logger = new MiddyLogger(lambdaLogger);
58+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
4859
logger.Log(LogLevel.Info, "hello world", new LogProperty("key", classToLog));
4960

5061
Approvals.Verify(receivedLog);
@@ -53,7 +64,7 @@ public void LogExtraPropertiesWithObject()
5364
[Fact]
5465
public void LogEnrichWithExtraProperties()
5566
{
56-
var logger = new MiddyLogger(lambdaLogger);
67+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
5768
logger.EnrichWith(new LogProperty("key", "value"));
5869
logger.Log(LogLevel.Info, "hello world");
5970

@@ -62,7 +73,7 @@ public void LogEnrichWithExtraProperties()
6273
[Fact]
6374
public void LogGlobalPropertiesAndExtraProperties()
6475
{
65-
var logger = new MiddyLogger(lambdaLogger);
76+
var logger = new MiddyLogger(lambdaLogger, lambdaContext);
6677
logger.EnrichWith(new LogProperty("key", "value"));
6778
logger.Log(LogLevel.Info, "hello world", new LogProperty("key2", "value2"));
6879

0 commit comments

Comments
 (0)