Skip to content

Lambda Logging improvements #2062

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

Merged
merged 4 commits into from
Jun 3, 2025
Merged

Lambda Logging improvements #2062

merged 4 commits into from
Jun 3, 2025

Conversation

normj
Copy link
Member

@normj normj commented May 2, 2025

Issue #, if available:
#2032

Description of changes:

  • In Amazon.Lambda.Logging.AspNetCore convert the ASP.NET Core log level into the Lambda log level and then pass the level into the logging method
  • Update Amazon.Lambda.TestUtitlies to have implementation of the newer logging methods
  • Add a new static global static logging method that takes in log level, message, parameters and exception.
  • Passing in exception in the global static logger was missing.

I had also wanted to update Amazon.Lambda.Logging.AspNetCore to support converting the log method to JSON if that was enabled in Lambda. Since we are missing the global static logging method that takes in exception I couldn't do that without losing the exception in the log. Once that new method added in RuntimeSupport in this PR is deployed to the managed runtimes I can go back and add the support.

I updated Amazon.Lambda.Logging.AspNetCore and Amazon.Lambda.TestUtitlies to target .NET 6.0 and .NET 8.0 instead of .NET Standard 2.0. That was needed so I have access to the parameterized logging methods. I didn't do it as a major version bump because I figured where else could you have run these packages then one of those targets. I don't feel to strong on that and if others feel I should do it as a major version bump I'm okay with that.

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

normj added 2 commits May 2, 2025 14:18
In Amazon.Lambda.Logging.AspNetCore convert the ASP.NET Core log level into the Lambda log level and then pass the level into the logging method
Update Amazon.Lambda.TestUtitlies to have implementation of the newer logging methods
Add a new static global static logging method that takes in log level, message, parameters and exception. Passing in exception in the global static logger was missing.
@normj normj requested a review from philasmar May 7, 2025 20:23
},
{
"Name": "Amazon.Lambda.Logging.AspNetCore",
"Type": "Minor",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would feel more comfortable making this a Major version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

},
{
"Name": "Amazon.Lambda.TestUtilities",
"Type": "Minor",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would feel more comfortable making this a Major version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -78,7 +88,6 @@ private static void LogWithLevelToConsole(string level, string message, params o
/// <param name="level">The log level of the message</param>
/// <param name="message">Message to log. The message may have format arguments.</param>
/// <param name="args">Arguments to format the message with.</param>
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these no longer needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the implementation in Amazon.Lambda.RuntimeSupport of these methods have been deployed to the managed runtimes. That were marked as preview till that deployment was done.

/// <param name="exception">Exception to include with the logging.</param>
/// <param name="message">Message to log. The message may have format arguments.</param>
/// <param name="args">Arguments to format the message with.</param>
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this here but not in others?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this PR adds the implementation of these methods in Amazon.Lambda.RuntimeSupport and we will need to wait till that change in Amazon.Lambda.RuntimeSupport gets deployed to managed runtime. Ideally I should have added these at the same time I did the versions that didn't take an Exception. That was a miss on my part that I'm fixing.

/// <param name="exception">Exception to include with the logging.</param>
/// <param name="message">Message to log. The message may have format arguments.</param>
/// <param name="args">Arguments to format the message with.</param>
[RequiresPreviewFeatures(ParameterizedPreviewMessage)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered above

}

internalLogger.LogDebug($"UCL : Retrieving type '{Types.LambdaLoggerTypeName}'");
var lambdaILoggerType = coreAssembly.GetType(Types.LambdaLoggerTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt this library NativeAOT compatible? Why are you using reflection here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UserCodeLoader is not Native AOT complaint but it is only used in class library programming model and you use executable programming model for Native AOT. At the top of the UserCodeLoader file is the RequiresUnreferencedCode attribute to say this code shouldn't be used for Native AOT.

}
}

internal static void SetCustomerLoggerLogAction(Assembly coreAssembly, Action<string, Exception, string, object[]> loggingWithAndExceptionLevelAction, InternalLogger internalLogger)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add docs for this method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

internalLogger.LogDebug($"UCL : Retrieving type '{Types.LambdaLoggerTypeName}'");
var lambdaILoggerType = coreAssembly.GetType(Types.LambdaLoggerTypeName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question abotut reflection

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered above

@@ -4,7 +4,7 @@

<PropertyGroup>
<Description>Amazon.Lambda.TestUtilties includes stub implementations of interfaces defined in Amazon.Lambda.Core and helper methods.</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment about moving to .net8 only

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just not ready to make that call yet. Especially not for this update.

Comment on lines 41 to 66
public void Log(string level, string message)
{
var formmattedString = $"{level}: {message}";
Buffer.AppendLine(formmattedString);
Console.WriteLine(formmattedString);
}

public void Log(string level, string message, params object[] args)
{
var builder = new StringBuilder();
builder.Append($"{level}: {message}");
if (args != null && args.Length > 0)
{
builder.AppendLine();
foreach (var arg in args)
{
builder.AppendLine($"\t{arg}");
}
}

var formmattedString = builder.ToString();
Buffer.AppendLine(formmattedString);
Console.WriteLine(formmattedString);
}

public void Log(string level, Exception exception, string message, params object[] args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add docs for these methods

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@GarrettBeatty GarrettBeatty self-requested a review May 9, 2025 17:21
@normj normj merged commit 1583a26 into dev Jun 3, 2025
5 of 6 checks passed
@normj normj deleted the normj/aspnet-logging branch June 3, 2025 03:33
@normj normj mentioned this pull request Jun 3, 2025
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.

3 participants