Log a request end

Hello every one. I have following problem:

My server should to log every request end. Every request contains request id and end request message should contains this id + status code .
A response middleware mechanics resolves my task almost. But there are several corner cases:

  • cliet disconnected. We In this case my server should to wrtite message with status code 499. But the middleware are not call.
  • request and resonse timeouts also cancelled the requst and the middleware are not call.

Are the ideas?