-
I recently spent a lot of time troubleshooting a problem that was caused by sending requests with invalid headers. The reason it took so long to troubleshoot was that there was nothing in my logs. I have a fairly elaborate system built up around collecting and processing logs, but in this case I could find nothing. The entire request was missing from all my logs. After a while I was able to ramp up logging to Debug and found this:
I was rather shocked to find that a bad request was logged as Debug. Now there is probably good reasons for this, but I wan't to be able to somehow catch and react to situations like these so I can more easily troubleshoot similar issues in the future. Despite having a global ExceptionFilterAttribute and now attempting to use app.UseExceptionHandler() in my Startup.cs (as discussed here https://www.strathweb.com/2018/07/centralized-exception-handling-and-request-validation-in-asp-net-core/) I can't seem to trigger anything when this error occurs. It seems that bad headers is such a low-level error it's handled deep inside Kestrel somewhere. So my question then is: can this somehow be captured in some way? If not can I atleast make it log bad requests as Error instead of Debug? Any suggestions will be appreciated... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Correct, if the request is considered malformed the server will refuse it immideately without surfacing it to the application. That's because the server has no confidence that it can properly handle the semantics of the request if it's malformed.
It's logged as debug because it's considered an external error that the application can't do anything about. We do something similar for client disconnect errors. What you can do today to surface these errors is to selectively enable those server logs. You know it's:
@shirhatti can that be done with log filters, or do you need a custom provider? It looks like filters can only get you down to the logger level (Kestrel), not specific event ids? |
Beta Was this translation helpful? Give feedback.
-
Just an FYI for others in the same situation. My workaround (since I'm using Serilog) was logging these events separately to a separate file.
|
Beta Was this translation helpful? Give feedback.
Correct, if the request is considered malformed the server will refuse it immideately without surfacing it to the application. That's because the server has no confidence that it can properly handle the semantics of the request if it's malformed.
It's logged as debug because it's considered an external error that the application can't do anything about. We do something similar for client disconnect errors.
What you can do today to surface these errors is to selectively enable those server logs. You know it's: