CloudWatch Logging Multiple lines for Exceptions
I am not sure if this is the right place for this issue - let me know if it's not.
I've encountered an error when trying to log Exceptions to CloudWatch. The issue is that the Exception gets stored in CloudWatch as separate log entries, and not in a single one. The Exception in CloudWatch looks like this:

I am simply using the build-in ASP.NET Core logging Framework, as described here: https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/cw-log-frameworks.html
I'm not able to reproduce this issue.
To be clear are you using the NuGet package Amazon.Lambda.Logging.AspNetCore which uses the CloudWatch Log stream that the Lambda service associates with the function or are you using AWS.Logger.AspNetCore which writes to a CloudWatch LogGroup you configured? In a Lambda environment Amazon.Lambda.Logging.AspNetCore is usually the appropriate one to use and it is what we configure by default with the latest versions of Amazon.Lambda.AspNetCoreServer
I wasn't - I was using Serilog.AspNetCore, as I have a requirement to also log to a file when running locally. However, I am not 100% set to using that, so I gave Amazon.Lambda.Logging.AspNetCore a shot.
Unfortunately, I am now not seeing any exceptions at all! To give you a bit of background, I have a Middleware class that will catch unhandled exceptions and create the correct response and log the exception. Here's the code for that:
public async Task Invoke(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception exception)
{
_logger.LogError(551, exception, "An unhandled exception occurred!");
_errorFactory.AddErrorDetail(Errors.GenericInternalServerError);
httpContext.Response.Clear();
httpContext.Response.ContentType = "application/json";
httpContext.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
using (var writer = new StreamWriter(httpContext.Response.Body))
{
new JsonSerializer().Serialize(writer, _errorFactory.Error);
await writer.FlushAsync().ConfigureAwait(false);
}
}
}
}
Here's the JSON Config:
"Lambda.Logging": {
"IncludeCategory": true,
"IncludeLogLevel": true,
"IncludeNewline": true,
"LogLevel": {
"Default": "Debug",
"Microsoft": "Debug"
}
}
I do see the exceptions in the Console when running locally:

But not in CloudWatch - only the message.

I did do a quick test by throwing an exception and the did seem to log the stack trace in the console, but I want to be able to handle all exceptions.
The main reason I was looking into Serilog is because I want to be able to display the EventId, and Correlation Id within CloudWatch, and local logs. I am trying to leverage the Request Id that the Lambda functions already has(or TraceIdentifier when running locally), and use that within my API, as mentioned here:
https://aws.amazon.com/blogs/compute/techniques-and-tools-for-better-serverless-api-logging-with-amazon-api-gateway-and-aws-lambda/
Does Amazon.Lambda.Logging.AspNetCore support this?
@vickityvik Do you have ASPNETCORE_ENVIRONMENT=Development set? If so then this is the same problem I describe in #242.
I am also having the same issue. This will only log the message, not the exception :-(
logger.LogError(551, exception, "An unhandled exception occurred!");
Same issue here. LambdaLogger is ignoring all parameters passed to Logger class, the only thing logged is the message :(
I have the same issue where CloudWatch agent reading my log files (generated by java application SL4J logger) is considering exception as a separate line item. Can anybody share tips on how to fix this?
A new version was released with my changes, on the options object you can set to include the exception in logs. Not sure if that's going to solve your problem though...