0

I have a c# console application running on .Net 4.5 framework. It has many outgoing HTTP POST and GET calls to various servers. Now I want to log the outgoing requests in my database with some custom code.

The project is already live so I do not want to touch the code of each and every http calls.

Is there any global trigger that I can implement in c# console application in .Net framework 4.5 that will execute on all the outgoing requests?

Yash
  • 356
  • 1
  • 5
  • 22
  • Assuming you only have 1 or a few places where the HttpClient is created, you can attach a custom HttpClientHandler, which you can add logging to. Alternately, you can enable http logging, but you might end up with too much logging then. – Neil Jan 04 '23 at 14:32
  • Does this answer your question? [Logging request/response messages when using HttpClient](https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient) – Neil Jan 04 '23 at 14:32
  • Hello @Neil, HttpClientHandler I tried but that is not working for me. I am not sure if that is not supported or I am implementing it wrong way. My project has many ClassLibrary project. Each project contains HTTP calls to different APIs. My console application is calling methods from those ClassLibrary projects based on needs. What do you suggest in this kind of scenario? – Yash Jan 04 '23 at 14:38
  • If your class libraries repeatedly call `new HttpClient`, then they are incorrect, and will fail in high-usage scenarios. The duplicate question gives you examples of both methods. – Neil Jan 04 '23 at 14:42
  • One execution of ConsoleApp will call any one ClassLibrary and any one HTTP call in it based on the parameters. How can I execute custom c# code with http logging? – Yash Jan 04 '23 at 14:55

0 Answers0