1

I want to filter requests based on ResponseCode i.e requests with 500, 400, 401 etc response status codes should not be logged with Audit.Net. I have used AuditMiddleware but it is not filtering 500, 400 and 401 requests responses.

Code:

 app.UseAuditMiddleware(_ => _
                .FilterByRequest(r => !r.Path.Value.EndsWith("favicon.ico") && r.HttpContext.Response.StatusCode==StatusCodes.Status200OK)
                .IncludeHeaders()
                .IncludeRequestBody()
                .IncludeResponseBody()
                .WithEventType("HTTP:{verb}:{url}"));

Then i called it in ConfigureApp method in StartUp.cs but it does not filter.

1 Answers1

1

The FilterByRequest() works as a pre-filter that is called before the action executes, so you won't have the response on the filter delegate.

The option to discard events based on the response is to do a post-filter via a custom action, for example on your startup:

using Audit.WebApi;

Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaving, scope =>
{
    var ev = scope.GetWebApiAuditAction();
    if (ev?.ResponseStatusCode == 500 || ev?.ResponseStatusCode == 400)
    {
        scope.Discard();
    }
});
thepirat000
  • 12,362
  • 4
  • 46
  • 72