Skip to main content
net6.0net8.0

Powertools for AWS Lambda (.NET) - Logging package.

Published
Mar 10, 2024
2y ago
Total Downloads
2.8M
2,799,338

Install

dotnet add package AWS.Lambda.Powertools.Logging --version 1.5.1
ProductVersions
.NET
net6.0net8.0

README

AWS.Lambda.Powertools.Logging

The logging utility provides a AWS Lambda optimized logger with output structured as JSON.

Key features

  • Capture key fields from Lambda context, cold start and structures logging output as JSON
  • Log Lambda event when instructed (disabled by default)
  • Log sampling enables DEBUG log level for a percentage of requests (disabled by default)
  • Append additional keys to structured log at any point in time

Read the docs

For a full list of features go to docs.powertools.aws.dev/lambda/dotnet/core/logging/

GitHub: https://github.com/aws-powertools/powertools-lambda-dotnet/

Sample Function

public class Function
{
    [Logging(LogEvent = true)]
    public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
        ILambdaContext context)
    {
        var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
        
        var lookupInfo = new Dictionary<string, object>()
        {
            {"LookupInfo", new Dictionary<string, object>{{ "LookupId", requestContextRequestId }}}
        };

        // Appended keys are added to all subsequent log entries in the current execution.
        // Call this method as early as possible in the Lambda handler.
        // Typically this is value would be passed into the function via the event.
        // Set the ClearState = true to force the removal of keys across invocations,
        Logger.AppendKeys(lookupInfo);

        Logger.LogInformation("Getting ip address from external service");

        var location = await GetCallingIp();

        var lookupRecord = new LookupRecord(lookupId: requestContextRequestId,
            greeting: "Hello Powertools for AWS Lambda (.NET)", ipAddress: location);

        try
        {
            await SaveRecordInDynamo(lookupRecord);
            
            return new APIGatewayProxyResponse
            {
                Body = JsonSerializer.Serialize(lookupRecord),
                StatusCode = 200,
                Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
            };
        }
        catch (Exception e)
        {
            Logger.LogError(e.Message);

            return new APIGatewayProxyResponse
            {
                Body = e.Message,
                StatusCode = 500,
                Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
            };
        }
    }
}

Sample output

{
    "cold_start": false,
    "xray_trace_id": "1-623d34cb-00c3698b02f11dc713442693",
    "lookup": {
        "lookup_id": "7d2ce9bb-c7d1-4304-9912-6078d276604f"
    },
    "function_name": "PowertoolsLoggingSample-HelloWorldFunction-hm1r10VT3lCy",
    "function_version": "$LATEST",
    "function_memory_size": 256,
    "function_arn": "arn:aws:lambda:ap-southeast-2:111111111111:function:PowertoolsLoggingSample-HelloWorldFunction-hm1r10VT3lCy",
    "function_request_id": "f5c3bae7-0e18-495a-9959-ef101c7afbc0",
    "timestamp": "2022-03-25T03:19:39.9322301Z",
    "level": "Information",
    "service": "powertools-dotnet-logging-sample",
    "name": "AWS.Lambda.Powertools.Logging.Logger",
    "message": "Getting ip address from external service"
}

Changes from Previous Version

v1.4.4v1.5.1
Added Frameworks
net8.0
Added Dependencies

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.