I have an ASP.NET service, which already has config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());
In there it traces the specific error, so nothing special.
I want to be a bit more specific about how the case of invalid URL is traced.
For example, if my service has a controllers/methods that correspond to various /api/controller/../method/..
paths in the request URL, I want to trace requests that don't match any controller/method with a special flag, to filter them easily - I don't want to see all the random /bot/probing.php
and hack/seeking.jsp
to (1) pollute my normal logs (2) get lost and not noticed in time.
Ideally, I'd also like to cut the connection and not respond with HTTP 500 to the caller, if that's possible
Requests that cannot be handled by WebAPI OWIN middleware, are passed to the next middleware in OWIN pipeline. It means that you can add a custom middleware after the WebAPI middleware to handle requests that do not match any controller/action pair.
Also if all your REST API endpoints have a base path of /api
you can map WebAPI and your custom middlewares to this base path to exclude requests to random URLs (like /bot/probing.php
) from processing.
An OWIN Startup class that configures the pipeline described above can be the following:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map(
"/api",
apiApp =>
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app
.UseWebApi(config)
.Use(async (ctx, next) =>
{
Trace.TraceError(
"WebApi routing error: {0}{1}",
ctx.Request.PathBase,
ctx.Request.Path);
await next();
});
});
}
}