sheepinwild
sheepinwild

Reputation: 541

How to properly log response http code in ASP.NET CORE 3.1 filters?

I want to log response http status code in filters? I have an action filter and an exception filter. I can log 500 responses in the exception filter. however the response object I get in the action filter is always 200 because at that time the response body is not written into.

I thought about using middleware however I cannot seem to get the Controller name and Action name in the middleware. Any suggestions?

Upvotes: 1

Views: 196

Answers (1)

sheepinwild
sheepinwild

Reputation: 541

I figured this out...

                var endpoint = httpContext.GetEndpoint();
            if (endpoint != null)
            {
                var controllerActionDescriptor = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
                if (controllerActionDescriptor != null)
                {
                    var controllerName = controllerActionDescriptor.ControllerName;
                    var actionName = controllerActionDescriptor.ActionName;
                }
            }

Upvotes: 1

Related Questions