aws-powertools / powertools-lambda-dotnet

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/dotnet/
MIT No Attribution
155 stars 24 forks source link

Docs: Improve Logger documentations typed logger #496

Open amirkaws opened 1 year ago

amirkaws commented 1 year ago

What were you searching in the docs?

I am looking for the documentation to show me how to use typed logger, and interface based logger rather than Logger. static methods.

public class Function
{
    private static ILogger _logger;

    public Function()
    {
        _logger = Logger.Create<Function>();
    }

    [Logging(LogEvent = true)]
    public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
              ILambdaContext context)
    {
        _logger.LogInformation(new { LogLocation = "FunctionHandler" }, "Test Message");

        ...
    }
}
{
    "cold_start": true,
    "xray_trace_id": "1-61b7add4-66532bb81441e1b060389429",
    "function_name": "test",
    "function_version": "$LATEST",
    "function_memory_size": 128,
    "function_arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
    "function_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72",
    "timestamp": "2021-12-13T20:32:22.5774262Z",
    "level": "Information",
    "service": "lambda-example",
    "name": "HelloWorld.Function",
    "message": "Collecting payment",
    "sampling_rate": 0.7,
    "correlation_id": "correlation_id_value"
}

Is this related to an existing documentation section?

No response

How can we improve?

Add a section in documentation related to typed logger, and another one for dependency injection

Got a suggestion in mind?

No response

Acknowledgment

rahulpnath commented 10 months ago

A couple of observations using the log method on the Logger instance

  1. Throws an exception System.Reflection.TargetParameterCountException: Parameter count mismatch, when using the LogInformation with parameters

    _logger.LogInformation("Testing with parameter Log Information Method {cityName}", cityName);

    Have to explicitly pass the parameters as an object[] for it to work like below.

    _logger.LogInformation("Testing with parameter Log Information Method {cityName}", new[] { cityName });
  2. Logged Parameters are not available in the output as destructured parameters. The above log statement outputs the below in CloudWatch

    {
    "cold_start": true,
    "xray_trace_id": "1-654bd325-0acb33237c954016773e537b",
    "function_name": "weathertest-LambdaEmptyFunction1FunctionGetWeather-zQceOL6olYxd",
    "function_version": "$LATEST",
    "function_memory_size": 256,
    "function_arn": "arn:aws:lambda:ap-southeast-2:189107071895:function:weathertest-LambdaEmptyFunction1FunctionGetWeather-zQceOL6olYxd",
    "function_request_id": "f32b5d3f-62eb-424c-9cbc-d5b0b48e3d0d",
    "timestamp": "2023-11-08T18:27:50.6013375Z",
    "level": "Information",
    "service": "service_undefined",
    "name": "LambdaEmptyFunction1.Function",
    "message": "Testing with parameter Log Information Method Brisbane"
    }

    I expected the cityName to be available in the log output.

Not sure if these are something that could be included as part of the documentation or am f I am missing something here? @amirkaws

hjgraca commented 10 months ago

Hi @rahulpnath thanks for raising the issue and thanks for the youtube video as well 😄 really cool.

So at the moment the way you add "addiitonal keys" is by calling the methods Logger.AppendKey or Logger.AppendKeys you can have a look in the docs.

The reason behind that approach was for the user experience to be the same across all Powertools runtimes. We now believe that we need a more .NET way to do logging, we are in the process of creating an RFC which will be similar to what you are alluding to, essentially resembling what ILogger does or what Serilog does.

Any input or feedback will be greatly appreciated, we don't have anything public yet, so if you have any ideas would love to hear them

hjgraca commented 6 months ago

Discussion: https://github.com/aws-powertools/powertools-lambda-dotnet/discussions/570