Skip to content

Add Counters & Logs for 404s and MapFallback #46404

@Tratcher

Description

@Tratcher

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

If a request reaches the end of the pipeline (e.g. isn't handled anywhere else) there is a default middleware that sends an empty 404 response.

if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
}

Similarly MapFallback catches any request that didn't map to another route.

public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoints, Delegate handler)
{
return endpoints.MapFallback("{*path:nonfile}", handler);
}

Problem: There's no visibility in the system for tracking how many requests are missing routes and why. This information would be needed to identify bad links, misconfigured routes, and monitor the customer experience.

Describe the solution you'd like

  • Add a meter/counter that tracks how many requests reach the 404 middleware.
  • Add a meter/counter that tracks how many requests reach the fallback route. This may already be covered by Add Meter/Counters to track hits for each route/endpoint #46361
  • Add an Info or Debug level log in each of these places that includes the following:
    • Scheme, host, port, path base, path
    • Method
    • Protocol
  • Use structured logging to allow low level filtering and avoid unnecessary allocations
  • These should be under granular log prefixes for targeted enablement.
    • Microsoft.AspNetCore.Routing.EndOfPipeline.UnexpectedRequest(scheme...
    • Microsoft.AspNetCore.Routing.Fallback.HandledRequest(scheme...

Additional context

The counters would be used to monitor the volume of unexpected requests, and then the logs could be enabled to determine the specific request types.

Having sampling support in the loggers would also help deal with high volume output.

Metadata

Metadata

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions