【问题标题】:Optional logging http requests and responseOptional logging http requests and response
【发布时间】:2022-12-27 21:15:38
【问题描述】:

Could somebody describe how achieve those goal: I have http client with methods. Sometimes server start send strange data, and I need to capture request and response. Injecting logger to every method in httpClient sounds awful. I need this log sometimes and amount of log information can be huge. So i need to switch logging for precise httpclient method capture data i.e. for a day and turn off log's without recompiling application. What I'm thought I can register delegate handler which get as option data from appsettings json where I store something which can identify my methods that should be logged. I think somebody already did that. And maybe I can use ready-made solution. If not I can't completely invent a way how map data from appsettings.json to info that delegate handler has. I mean what way i should store in example full URL or maybe some another data on what i can identify what type of http request sended.

【问题讨论】:

    标签: c# http delegates .net-7.0


    【解决方案1】:

    Injecting logger to every method in httpClient sounds awful. I need this log sometimes and amount of log information can be huge.

    What you need is an application wide setting called 'debug', which you can enable/disable when starting the application.

    If it is enabled, it will log all output. So you can enable it for a day, then reboot your application with the parameter disabled. You can also re-use this for other potential problem areas.

    Alternatively, you can build conditional statements in the return method; if the output is not exactly X, then log it.

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 2019-03-26
      • 2021-06-17
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      相关资源
      最近更新 更多