Logging on AWS Lambda with .NET Core

less than 1 minute read

The easiest way to output logs from the function code is using the Console class or LambdaLogger clas that writes to stdout / stderr.

LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables()));
LambdaLogger.Log("CONTEXT: " + JsonConvert.SerializeObject(context));
LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(invocationEvent));

If you want to use ILogger, which is the standard logging interface, use Microsoft.Extensions.Logging.Console

Services.AddLogging(x => x
    .AddFilter("", LogLevel.Information)
    .AddConsole());

In .NET Core 3.1, you can use ILoggerFactory to set up the logging. Please refer to “Non-host console app

Categories:

Updated:

Comments