0

I have a Web API application which not only receives calls to its own endpoints but also makes calls to endpoints of another API. My goal is to log all the incoming & outgoing HTTP requests. It is quite obvious how to do it with an ActionFilter - Intercept all WebApi calls before the route matching occurs - but it would only work with incoming HTTP requests and that's not enough for me. I also need to intercept requests that my API makes to another API, like this one:

var response = await client.GetAsync("/api/my/endppoint/name");

I haven't found any examples despite googling a lot. What would be the best way?

deepo
  • 117
  • 7
  • 1
    [Http Logging is available out of the box](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-7.0). It's enough to add `app.UseHttpLogging();`. If you use IIS or another web server, you can configure HTTP logging on the server itself – Panagiotis Kanavos Feb 22 '23 at 13:11
  • You should use HttpClient constructor with a handler. See some ideas [here](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers). – Rogerson Nazário Feb 22 '23 at 13:13
  • Thanks @PanagiotisKanavos. Will it also log outgoing API calls? Can you configure it to log to e.g. an SQL table? – deepo Feb 22 '23 at 13:30
  • 1
    You *can* log every network operation by enabling network tracing. That will log even socket operations though. It can get very noisy. In all production situations, what's really needed is proper application logging, not logging every possible call – Panagiotis Kanavos Feb 22 '23 at 13:35
  • @PanagiotisKanavos I need to log it to an SQL table because HTTP calls will contain sensitive data that I can't log somewhere like elastic. – deepo Feb 22 '23 at 13:48
  • Maybe there was a misunderstanding, I don't want to log any database calls. I want to log all incoming/outgoing HTTP calls the way UseHttpLogging does it, but save them in my own SQL table on MS SQL server. – deepo Feb 22 '23 at 13:52

2 Answers2

0

You can log all incoming requests using ActionFilter as you mentioned or using Middlware if you are using .Net Core

Regarding outcoming API calls I don't think that you can log it using Framework tools.

I would suggest logging manually all calls you make. You can create a wrapper over HttpClient.

0

To intercept outgoing calls, you can do something like this:

public class CustomDelegatingHandler : DelegatingHandler
{   
    public CustomDelegatingHandler(DelegatingHandler handler)
    {
        InnerHandler = handler;
    }

    // You can do the same for GetAsync, PostAsync, etc...
    protected override async Task<HttpResponseMessage> SendAsync(
      HttpRequestMessage request, CancellationToken cancellationToken)
    {
        //Perform the log here

        return await InnerHandler.SendAsync(request, cancellationToken);
    }
}

And your client instantiation should be:

var client = new HttpClient(new CustomDelegatingHandler());
  • The constructor for your CustomDelegatingHandler requires an argument of type DelegatingHandler though, so you may want to update your instantiation example with an argument passed, perhaps 'new HttpClientHandler()'. (I didn't want to make the edit as I'm honestly ignorant to etiquette on that, and I didn't want to change any perceived intent on your part, so I decided a comment was the safest option) – Ryan Faricy Aug 25 '23 at 18:20