Microsoft.Extensions.Logging.ILogger interface support for Lambda #1232
Labels
feature-request
A feature should be added or improved.
module/lambda-client-lib
needs-investigation
needs-review
Describe the feature
The
dotnet6
Lambda runtime should offer a log provider that supports theMicrosoft.Extensions.Logging.ILogger
logging interface.Use Case
We have lots of Lambdas written in C# that makes heavy uses of .NET built-in Dependency injection (aka
IServiceProvider
). The typical usage is to set up the DI container in the function's constructor, and uses it to resolve the classes in the function handler method. We believe this is the "right" way, as creating a new DI container per invocation is too expensive.The provided
ILambdaLogger
interface used for logging however is wrapped insideILambdaContext
which is provided per-invocation, making it impossible to set up in the with the DI pipeline (without some ugly work-around). We believe usingIServiceProvider
is very common among .NET customers.On top of that
ILamdaLogger
misses some features compared toILogger
:AWS_LAMBDA_HANDLER_LOG_LEVEL
toWarning
, ourDebug
andInfo
messages are stilled formed. This is not very efficient.Proposed Solution
We propose that the runtime offers an implementation of
ILoggerProvider
(akaLambdaLogProvider
, see Microsoft docs).There is currently an existing implementation in
Amazon.Lambda.Logging.AspNetCore
which delegates back toLambdaLogger
but we would like to see it implemented in the core runtime so that other app types can benefit from it.We have a PoC implementation here https://github.com/dhhoang/LambdaCustomLogger. Compared to the implementation in
Amazon.Lambda.Logging.AspNetCore
, it has some added features:ILogHandler
._LAMBDA_TELEMETRY_LOG_FD
). The currentLambdaLogger
has a bug that results in multi-line logs ending up as several CloudWatch Logs entries (instead of one with several lines). For example, when logging exception with defaultLambdaLogger
:With our
CustomLogger
_LAMBDA_TELEMETRY_LOG_FD
which should make it more efficient.Other Information
We are interested in submitting PR for this feature.
Our current idea is to allow users configure a log handler in the DI like so
How the actual JSON logs (or any other format) look will be completely up to the handler implementation. Anyone can implement a log handler to support custom format. The default handler can output same format as is written by lambda today. We are interested in hearing feedback about what formats should be supported by default.
Acknowledgements
AWS .NET SDK and/or Package version used
"Amazon.Lambda.Core" Version="2.1.0"
Targeted .NET Platform
.NET 6
Operating System and version
AmazonLinux
The text was updated successfully, but these errors were encountered: