14
14
*/
15
15
16
16
using System ;
17
+ using System . Collections . Generic ;
17
18
using System . IO ;
18
19
using System . Linq ;
19
20
using System . Reflection ;
@@ -53,6 +54,11 @@ public class LoggingAspect
53
54
/// The correlation identifier path
54
55
/// </summary>
55
56
private string _correlationIdPath ;
57
+
58
+ /// <summary>
59
+ /// Paths to arbitrary values to extract from the input object and the names of the keys to store them in
60
+ /// </summary>
61
+ private IEnumerable < ( string Path , string KeyName ) > _extractedKeyPaths ;
56
62
57
63
/// <summary>
58
64
/// The Powertools for AWS Lambda (.NET) configurations
@@ -136,6 +142,7 @@ public void OnEntry(
136
142
137
143
var logEvent = trigger . LogEvent ;
138
144
_correlationIdPath = trigger . CorrelationIdPath ;
145
+ _extractedKeyPaths = trigger . ExtractedKeyPaths ;
139
146
_clearState = trigger . ClearState ;
140
147
141
148
Logger . LoggerProvider = new LoggerProvider ( _config , _powertoolsConfigurations , _systemWrapper ) ;
@@ -153,6 +160,7 @@ public void OnEntry(
153
160
CaptureXrayTraceId ( ) ;
154
161
CaptureLambdaContext ( eventArgs ) ;
155
162
CaptureCorrelationId ( eventObject ) ;
163
+ CaptureExtractedKeyPaths ( eventObject ) ;
156
164
if ( logEvent || _powertoolsConfigurations . LoggerLogEvent )
157
165
LogEvent ( eventObject ) ;
158
166
}
@@ -224,48 +232,73 @@ private void CaptureLambdaContext(AspectEventArgs eventArgs)
224
232
/// <param name="eventArg">The event argument.</param>
225
233
private void CaptureCorrelationId ( object eventArg )
226
234
{
227
- if ( string . IsNullOrWhiteSpace ( _correlationIdPath ) )
235
+ CaptureKeyFromPath ( eventArg , _correlationIdPath , LoggingConstants . KeyCorrelationId ) ;
236
+ }
237
+
238
+ /// <summary>
239
+ /// Captures the extracted key paths.
240
+ /// </summary>
241
+ /// <param name="eventArg">The event argument.</param>
242
+ private void CaptureExtractedKeyPaths ( object eventArg )
243
+ {
244
+ if ( ! _extractedKeyPaths ? . Any ( ) ?? true )
245
+ return ;
246
+ foreach ( var ( path , keyName ) in _extractedKeyPaths )
247
+ {
248
+ CaptureKeyFromPath ( eventArg , path , keyName ) ;
249
+ }
250
+ }
251
+
252
+ /// <summary>
253
+ /// Captures an arbitrary value from the path and stores it in a key
254
+ /// </summary>
255
+ /// <param name="eventArg">The event argument.</param>
256
+ /// <param name="path">The path to the input data.</param>
257
+ /// <param name="keyName">The name of the key to store the data in.</param>
258
+ private void CaptureKeyFromPath ( object eventArg , string path , string keyName )
259
+ {
260
+ if ( string . IsNullOrWhiteSpace ( path ) || string . IsNullOrWhiteSpace ( keyName ) )
228
261
return ;
229
262
230
- var correlationIdPaths = _correlationIdPath
263
+ var paths = path
231
264
. Split ( CorrelationIdPaths . Separator , StringSplitOptions . RemoveEmptyEntries ) ;
232
265
233
- if ( ! correlationIdPaths . Any ( ) )
266
+ if ( ! paths . Any ( ) )
234
267
return ;
235
268
236
269
if ( eventArg is null )
237
270
{
238
271
if ( IsDebug ( ) )
239
272
_systemWrapper . LogLine (
240
- "Skipping CorrelationId capture because event parameter not found." ) ;
273
+ $ "Skipping { keyName } capture because event parameter not found.") ;
241
274
return ;
242
275
}
243
276
244
277
try
245
278
{
246
- var correlationId = string . Empty ;
279
+ var keyValue = string . Empty ;
247
280
248
281
var jsonDoc =
249
282
JsonDocument . Parse ( PowertoolsLoggingSerializer . Serialize ( eventArg , eventArg . GetType ( ) ) ) ;
250
283
251
284
var element = jsonDoc . RootElement ;
252
285
253
- for ( var i = 0 ; i < correlationIdPaths . Length ; i ++ )
286
+ for ( var i = 0 ; i < paths . Length ; i ++ )
254
287
{
255
288
// For casing parsing to be removed from Logging v2 when we get rid of outputcase
256
289
// without this CorrelationIdPaths.ApiGatewayRest would not work
257
290
var pathWithOutputCase =
258
- _powertoolsConfigurations . ConvertToOutputCase ( correlationIdPaths [ i ] , _config . LoggerOutputCase ) ;
291
+ _powertoolsConfigurations . ConvertToOutputCase ( paths [ i ] , _config . LoggerOutputCase ) ;
259
292
if ( ! element . TryGetProperty ( pathWithOutputCase , out var childElement ) )
260
293
break ;
261
294
262
295
element = childElement ;
263
- if ( i == correlationIdPaths . Length - 1 )
264
- correlationId = element . ToString ( ) ;
296
+ if ( i == paths . Length - 1 )
297
+ keyValue = element . ToString ( ) ;
265
298
}
266
299
267
- if ( ! string . IsNullOrWhiteSpace ( correlationId ) )
268
- Logger . AppendKey ( LoggingConstants . KeyCorrelationId , correlationId ) ;
300
+ if ( ! string . IsNullOrWhiteSpace ( keyValue ) )
301
+ Logger . AppendKey ( keyName , keyValue ) ;
269
302
}
270
303
catch ( Exception e )
271
304
{
0 commit comments