Skip to content

Amazon.Lambda.Logging.AspNetCore create parameterized JSON log messages #2083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

normj
Copy link
Member

@normj normj commented Jun 3, 2025

Description of changes:

When using the ILogger in an ASP.NET Core Lambda function or any Lambda function using Microsoft.Extensions.Loggingfor logging currently don't have the parameters for the log messages added as properties in the JSON log message. This PR updatesAmazon.Lambda.Logging.AspNetCoreto see if JSON logging is enabled. If so it deconstructs the incoming log message intoAmazon.Lambda.Logging.AspNetCore`'s ILogger implementation into a message template and parameter collection. Then send those deconstructed pieces into RuntimeSupport to create the standard JSON Lambda log message with the log parameters as JSON properties.

For example the following log message:

[HttpGet("add/{x}/{y}")]
public int Add(int x, int y)
{
    _logger.LogInformation("{x} plus {y} is {sum}", x, y, x + y);
    return x + y;
}

Gets converted into the following JSON log message:

{
  "timestamp": "2025-06-03T16:41:50.400Z",
  "level": "Information",
  "requestId": "6e8db1d2-ce80-4ad5-8207-15d405fdd7db",
  "traceId": "Root=1-683f25ce-570d95815c4592d350eb955b;Parent=48f2bf85b1a86683;Sampled=0;Lineage=1:2f30c19f:0",
  "message": "3 plus 4 is 7",
  "x": 3,
  "y": 4,
  "sum": 7
}

The PR is currently in PR because we need the latest version of Amazon.Lambda.RuntimeSupport to be deployed to the managed runtime. That version has a new logging API that is required for this to work.

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

@normj normj marked this pull request as draft June 3, 2025 16:51
@normj normj force-pushed the normj/aspnet-logging-json branch from 9fd5b5e to 399ed21 Compare June 3, 2025 16:58
…t to JSON

This means the log message sent through the ILogger is converted into a parameterized log message allowing RuntimeSupport to create the JSON document
@normj normj force-pushed the normj/aspnet-logging-json branch from 399ed21 to 16bf25a Compare June 3, 2025 19:09
@normj normj requested review from philasmar and GarrettBeatty June 3, 2025 19:12
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.

1 participant