2

I'm using Serilog and Serilog.Sinks.Seq to send events to Seq. Some events are arriving in the Seq log, but some are missing.

How can I ensure that all events sent during execution of my Lambda function arrive in Seq?

liammclennan
  • 5,295
  • 3
  • 34
  • 30

1 Answers1

2

Serilog buffers events for performance reasons, so if an application exits suddenly it can fail to forward all the log entries, as you suggested.

Before your lambda function completes try calling Log.CloseAndFlush(); to ensure any buffered events are processed before the application exits. If you are using ILogger instead of the static Log class then you will need to dispose the ILogger instead of calling CloseAndFlush().

Serilog's Lifecycle of Loggers page provides more detail if you are interested.

liammclennan
  • 5,295
  • 3
  • 34
  • 30
  • Thanks for posting this - I have the same problem. How do you know when the Lambda exits? I placed the Close&Flush() at the end of the lambda, but I found out that after the first message is processed, no more logs appear. My understanding is its because the lambda has not been disposed. It will sit there for a while waiting for new requests and then dispose after some time. How do you hook into the 'dispose' event of a lambda? ManyThanks – biso Apr 26 '22 at 08:04
  • The only reason I think that would happen is if your function throws an exception. Try using `try {} finally { Log.CloseAndFlush(); }` – liammclennan May 04 '22 at 12:47
  • The lambda stays hot for a little while, so if you close and flush, that closes the log stream? Meaning that if a hot lambda is triggered again, the log stream is closed and no more logs will be sent. I've just hit this problem. This would be a good usecase for https://github.com/serilog/serilog/issues/1111#issuecomment-452522381 (this is an ASP.Net Core Server Lambda btw https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.AspNetCoreServer - no hook for disposal, AFAIK) Work around is logging JSON Formatted logs with Console Sink, which show up in CloudWatch. – legas May 23 '22 at 12:25
  • @legas if a hot lambda is triggered again it will create a new log client, use it, close it. – liammclennan May 24 '22 at 22:44
  • 1
    Hey @biso - if you're still looking for a solution on this, you can create a Lambda function triggered by Cloudwatch log events. Here is the code: https://gist.github.com/larenelg/326728ada62742dbd128b5ff202c451e – legas May 25 '22 at 06:34