Skip to content

Commit f5ae52d

Browse files
committed
Added CorrelationId Property and method
1 parent 00c0e26 commit f5ae52d

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Logger.Scope.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using AWS.Lambda.Powertools.Logging.Internal;
45
using AWS.Lambda.Powertools.Logging.Internal.Helpers;
56

67
namespace AWS.Lambda.Powertools.Logging;
@@ -13,6 +14,22 @@ public static partial class Logger
1314
/// <value>The scope.</value>
1415
private static IDictionary<string, object> Scope { get; } = new Dictionary<string, object>(StringComparer.Ordinal);
1516

17+
/// <summary>
18+
/// Gets the correlation identifier from the log context.
19+
/// </summary>
20+
/// <value>The correlation identifier, or null if not set.</value>
21+
public static string CorrelationId
22+
{
23+
get
24+
{
25+
if (Scope.TryGetValue(LoggingConstants.KeyCorrelationId, out var value))
26+
{
27+
return value?.ToString();
28+
}
29+
return null;
30+
}
31+
}
32+
1633
/// <summary>
1734
/// Appending additional key to the log context.
1835
/// </summary>

libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ public static void Log(this ILogger logger, LogLevel logLevel, Exception excepti
170170

171171
#endregion
172172

173+
/// <summary>
174+
/// Gets the correlation identifier from the log context.
175+
/// </summary>
176+
/// <param name="logger">The logger instance.</param>
177+
/// <returns>The correlation identifier, or null if not set.</returns>
178+
public static string GetCorrelationId(this ILogger logger)
179+
{
180+
return Logger.CorrelationId;
181+
}
182+
173183
/// <summary>
174184
/// Appending additional key to the log context.
175185
/// </summary>

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Attributes/LoggingAttributeTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,53 @@ public void When_Setting_SamplingRate_Should_Add_Key()
357357
));
358358
}
359359

360+
[Fact]
361+
public void CorrelationId_Property_Should_Return_CorrelationId()
362+
{
363+
// Arrange
364+
var correlationId = Guid.NewGuid().ToString();
365+
366+
// Act
367+
_testHandlers.CorrelationCloudWatchEventCustomPath(new CloudWatchEvent<CwEvent>
368+
{
369+
Detail = new CwEvent
370+
{
371+
CorrelationId = correlationId
372+
}
373+
});
374+
375+
// Assert - Static Logger property
376+
Assert.Equal(correlationId, Logger.CorrelationId);
377+
}
378+
379+
[Fact]
380+
public void CorrelationId_Extension_Should_Return_CorrelationId_Via_ILogger()
381+
{
382+
// Arrange
383+
var correlationId = Guid.NewGuid().ToString();
384+
385+
// Act
386+
_testHandlers.CorrelationIdExtensionTest(new CloudWatchEvent<CwEvent>
387+
{
388+
Detail = new CwEvent
389+
{
390+
CorrelationId = correlationId
391+
}
392+
});
393+
394+
// Assert - The test handler will verify the extension method works
395+
Assert.Equal(correlationId, Logger.CorrelationId);
396+
}
397+
398+
[Fact]
399+
public void CorrelationId_Should_Return_Null_When_Not_Set()
400+
{
401+
// Arrange - no correlation ID set
402+
403+
// Act & Assert
404+
Assert.Null(Logger.CorrelationId);
405+
}
406+
360407
[Fact]
361408
public void When_Setting_Service_Should_Update_Key()
362409
{

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/TestHandlers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ public void CorrelationCloudWatchEventCustomPath(CloudWatchEvent<CwEvent> cwEven
7171
{
7272
}
7373

74+
[Logging(CorrelationIdPath = "/detail/correlationId")]
75+
public void CorrelationIdExtensionTest(CloudWatchEvent<CwEvent> cwEvent)
76+
{
77+
// Test that the ILogger extension method works
78+
var logger = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { }).CreateLogger(nameof(TestHandlers));
79+
var correlationIdFromExtension = logger.GetCorrelationId();
80+
81+
// Verify it matches the static property
82+
if (correlationIdFromExtension != Logger.CorrelationId)
83+
{
84+
throw new Exception("Extension method returned different value than static property");
85+
}
86+
}
87+
7488
[Logging(CorrelationIdPath = "/headers/my_request_id_header")]
7589
public void CorrelationIdFromString(TestObject testObject)
7690
{

0 commit comments

Comments
 (0)