Skip to main content

Adding HTTP Request and Response Logging in C# v4 SDKs (Beta)

· 2 min read

C# v4 SDKs now log HTTP requests and responses through the standard Microsoft.Extensions.Logging abstractions. Logging is configured once on the client options and can be adjusted per call through the request options argument every endpoint accepts.

Details​

The Logging property on the client options takes a LoggingOptions. Give it an ILoggerFactory and the SDK writes the request line, the response line with its status and duration, retry notices and terminal failures. Request headers, response headers and the request body are off by default and switched on with LogRequestHeaders, LogResponseHeaders and LogRequestBody.

Query parameters and headers are redacted by default, Authorization included, and RedactedKeys, RedactedHeaders and UnmaskHeaders adjust what is masked. A client registered with Add{Api}Client picks up the container's ILoggerFactory automatically. A client you construct yourself logs nothing until you set a factory, or until an environment variable named after the client, such as PAYMENTSCLIENT_LOG, is set to info, debug or trace.

Every generated endpoint method takes an optional RequestOptions argument whose LogLevel sets the verbosity for that one call without touching the client configuration.

Usage Example​

Pass your application's logger factory to the client, then raise the detail for a single call when you need it:

var options = new PaymentsClientOptions
{
Logging = new LoggingOptions
{
LoggerFactory = loggerFactory,
LogRequestHeaders = true,
LogResponseHeaders = true,
},
};
var client = new PaymentsClient(httpClient, options);

var payment = await client.Payments.GetPayment(
new GetPaymentRequest { PaymentId = "pay_123" },
requestOptions: new RequestOptions { LogLevel = LogLevel.Trace });

LogRequestBody writes JSON bodies unredacted, so leave it off on endpoints that carry personal or payment data.

This feature is available in the C# v4 SDK (beta). Generate it from the APIMatic CLI:

apimatic sdk generate --language=csharp --codegen-version=v4 --stability=beta

For the full set of flags and options, see the APIMatic CLI documentation.