Skip to content

feat(logging): Structured scope support for Lambda JSON log format in Amazon.Lambda.Logging.AspNetCore - #2530

Open
Lanayx wants to merge 3 commits into
aws:masterfrom
Lanayx:master
Open

feat(logging): Structured scope support for Lambda JSON log format in Amazon.Lambda.Logging.AspNetCore#2530
Lanayx wants to merge 3 commits into
aws:masterfrom
Lanayx:master

Conversation

@Lanayx

@Lanayx Lanayx commented Aug 14, 2026

Copy link
Copy Markdown

Scope properties were silently dropped when AWS_LAMBDA_LOG_FORMAT=JSON. Addresses aws/aws-lambda-dotnet#2122. The final contribution should target aws/aws-lambda-dotnet:master. Closes #2122.

Changes

  • LambdaILogger.cs — In the JSON format path, when IncludeScopes=true, iterates the ScopeProvider and for each scope implementing IEnumerable<KeyValuePair<string, object>>, appends {key} placeholders to the message template and the corresponding values to the parameters array. This is necessary because the Lambda JSON formatter (RuntimeSupport) matches template placeholders positionally to args to emit named JSON properties — values without a matching placeholder are ignored. Non-structured scopes (plain strings etc.), null keys, and {OriginalFormat} entries are silently skipped. Non-JSON / scopes-disabled paths are unchanged.

  • LoggingTests.cs — Six focused tests covering: single KV scope, nested scopes, scopes disabled, no-scope template preservation, null scope value, and non-structured scope.

  • README.md — Documents structured scope behavior in Lambda JSON mode.

Example

// AWS_LAMBDA_LOG_FORMAT=JSON, IncludeScopes=true
using (logger.BeginScope(new Dictionary<string, object> { ["RequestId"] = "abc-123", ["UserId"] = 42 }))
{
    logger.LogInformation("Order {OrderId} placed", orderId);
    // Emits JSON with RequestId, UserId, and OrderId as named properties.
}

Scope properties are appended after message-template properties, so message positional args are unaffected. Nested scopes are collected in provider-enumeration order (outermost first).

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copilot AI and others added 3 commits August 14, 2026 03:07
When AWS_LAMBDA_LOG_FORMAT=JSON and IncludeScopes=true, scope state objects
implementing IEnumerable<KeyValuePair<string, object>> have their key/value
entries appended to the message template (as {key} placeholders) and the
parameters array, so they are emitted as named JSON properties by the Lambda
JSON formatter.

Non-structured scopes are silently ignored. Existing behavior (scopes disabled,
text-format logging, no scopes) is unchanged.

Closes aws#2122

Co-authored-by: Lanayx <3329606+Lanayx@users.noreply.github.com>
…port

feat(logging): Structured scope support for Lambda JSON log format in Amazon.Lambda.Logging.AspNetCore
@Lanayx
Lanayx requested review from a team as code owners August 14, 2026 03:25
@Lanayx
Lanayx requested review from GarrettBeatty and normj August 14, 2026 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support scopes with Amazon.Lambda.Logging.AspNetCore JSON logger

2 participants