Hi
I want to write logs in AWS cloudwatch using lambda
I'm using ILambdalogger but getting error in logs
public string FunctionHandler(string input, ILambdaContext context)
{
var logger = context.Logger;
sendLog(logger);
return input.ToUpper();
}
public static void sendLog(ILambdaLogger lambdaLogger)
{
resultToFile.errors = new List
resultToFile.errors.Add(new ErrorModel
{
CrDatTime = CommonHelper.ConvertDateTime_UTCtoEST(),
LogLevel = LogLevel.Information.ToString(),
MsgDetails = "1. AWS FunctionHandler Initialized",
SourceMethodName = "FunctionHandler"
});
if (resultToFile.errors != null)
{
lambdaLogger.LogInformation("Data executed successfully");
}
else
{
lambdaLogger.LogError("Error");
}
}
Please help.

Deepak RawatPosted Jun 22, 2023, 10:55 AM
To write logs to AWS CloudWatch using Lambda and
ILambdaLogger, you need to make sure your Lambda function has the necessary permissions to write logs to CloudWatch. Here are the steps you can follow to resolve the issue:Ensure your Lambda function has an IAM role attached that grants it permissions to write logs to CloudWatch. The role should have the
AWSLambdaBasicExecutionRolepolicy attached, which includes the necessary permissions for writing logs.Confirm that you have configured the appropriate CloudWatch log group and log stream for your Lambda function. You can do this through the AWS Management Console or by using the AWS CLI.
Check if you have included the necessary NuGet package in your Lambda function project. To use
ILambdaLoggerwith CloudWatch logs, you need to include theAWS.Logger.AspNetCorepackage. Make sure it is added to your project's dependencies.Verify that you are using the correct logging methods provided by
ILambdaLogger. In your code, you are usingLogInformationandLogError, which are not part ofILambdaLogger. Instead, useLogLineto write log messages.Here's an updated version of your code that addresses the above points:
By using
LogLineinstead ofLogInformationandLogError, you will be able to write log messages to CloudWatch. Additionally, the code now includes error handling to log any exceptions that may occur during the logging process.Make sure to deploy the updated Lambda function and check the CloudWatch logs for any errors or messages logged by your function.
Amit MohantyPosted Jun 22, 2023, 10:27 AM
Once you have the necessary permissions, you can modify your code to write logs to CloudWatch using the
ILambdaLoggerinterface.