0

I have an ASP.NET Core middleware component that is timing out within its public async Task Invoke(HttpContext context) method. After requesting the middleware and waiting about two minutes, the site sends this message: "HTTP Error 502.3 - Bad Gateway." But the exception does not continue up the middleware chain, so the exception cannot be caught by my error logging middleware.

I'd like to log and e-mail it the same way I handle my other exceptions. The error might get logged in the stdout log, and that's nice, but I also need the e-mail sent. So where can information about errors like this be received and handled? Thank you.

user1325179
  • 1,535
  • 2
  • 19
  • 29
  • Can you post the actual code of the middleware? Some exceptions cannot be caught. – Camilo Terevinto Dec 15 '17 at 00:05
  • It could be any middleware that takes a long time. Even `Thread.Sleep(TimeSpan.FromDays(1))` would do it. – user1325179 Dec 15 '17 at 00:56
  • Well, that would actually kill the thread so no wonder you get a timeout. – Camilo Terevinto Dec 15 '17 at 01:00
  • I'm not wondering why I get a timeout. `Thread.Sleep(TimeSpan.FromMinutes(10))` has the same effect. I'm wondering how to handle timeouts so I can log and e-mail information about them when they occur. – user1325179 Dec 15 '17 at 01:07
  • `Thread.Sleep` effectively puts the thread to sleep, nothing else can run in the meanwhile. No other middleware, task, request status, etc – Camilo Terevinto Dec 15 '17 at 01:08
  • Yes, I know. So it will cause a timeout error. I'm just giving you an example. It could be a really long SQL query. The question is not what kinds of code cause timeouts. That's obvious. I want to know where it's logged and how I can handle the error. – user1325179 Dec 15 '17 at 01:14
  • If you are doing that in a middle-ware, you are doing it completely wrong. Middle-ware are not for that kind of work. You cannot handle a request timeout error in a middleware, AFAIK. – Camilo Terevinto Dec 15 '17 at 01:18
  • The timeout is triggering in the underlying server, it's not an exception from the pipeline that you can catch. – Tratcher Dec 15 '17 at 01:42
  • 1
    @CamiloTerevinto, of course it's wrong if it takes that long. But I need to know if it happens. It shouldn't happen, but it could. Something that some middleware depends on might take an unusually long time to execute, but it will fail silently, at least from my code's perspective. A user might notice. Middleware can timeout, and I'd like to know when it does. – user1325179 Dec 15 '17 at 04:18
  • @Tratcher, you seem to understand my question. And I guess the answer is that it's impossible. ASP.NET 4 had the Application_Error event for this kind of thing, but ASP.NET Core can't seem to catch a timeout. Thank you. – user1325179 Dec 15 '17 at 04:19
  • @user1325179 Because it's not ASP.NET Core that's throwing the timeout, it's the underlying server. `Application_Error` does not catch 100% of the Exceptions, so it's not as easy as that. – Camilo Terevinto Dec 15 '17 at 09:39
  • @CamiloTerevinto, I agree. So perhaps nothing can be done to handle them at this point. Thank you. – user1325179 Dec 15 '17 at 14:10

0 Answers0