Add support to log exceptions with custom properties
If i have exception with custom property, like
public class MyCustomException : Exception
{
public string StatusCode { get; set; }
public MyCustomException()
{
}
public MyCustomException(string message)
: base(message)
{
}
public MyCustomException(string message, Exception inner)
: base(message, inner)
{
}
public MyCustomException(string message, string statusCode)
: base(message)
{
this.StatusCode = statusCode;
}
}
and if lambda function throws exception as
var result = await CallBackend();
if(result.Status != "success")
{
throw new MyCustomException("Error in backend",result.StatusCode);
}
In this case lambda does not serialize StatusCode property of MyCustomException. So in the CloudWatch i don't see StatusCode
Couple of workarounds 1>include statuscode in the exception message itself -> it may work for this scenario, but would not work if i also want to include more details related to error.
2>create custom exception for each status code -> we don't know the status code in advance
Currently i am assuming exception is serialized and returned as modeled JSON object.
Would it be possible to include result of Exception.ToString() method as additional property?
That would allow developer to include additional details by simply overriding ToString() method in custom exception.
You could have a look at using Serilog with the Console sink to customise the output https://github.com/serilog/serilog-sinks-console
I've added a PR to address including exceptions: https://github.com/aws/aws-lambda-dotnet/pull/401
Closing based on the PR that was merged long ago