aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Add support to log exceptions with custom properties

Open laksh-parab opened this issue 7 years ago • 3 comments

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.

laksh-parab avatar Jan 31 '19 23:01 laksh-parab

You could have a look at using Serilog with the Console sink to customise the output https://github.com/serilog/serilog-sinks-console

andymac4182 avatar Feb 03 '19 03:02 andymac4182

I've added a PR to address including exceptions: https://github.com/aws/aws-lambda-dotnet/pull/401

karpikpl avatar Feb 19 '19 15:02 karpikpl

Closing based on the PR that was merged long ago

klaytaybai avatar Apr 01 '20 22:04 klaytaybai