If you want to change all requests, you can customize the API clients, for example, by using a custom HTTP client, changing the user agent information, or changing the default timeouts.

To customize individual requests, for example, see Request options.

Logging

You can set a custom logger to enable more or less logging output. To run this example, you need to install an additional package:

1
dotnet add package Microsoft.Extensions.Logging.Console

Now you can pass a loggerFactory to the client, when creating it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using Algolia.Search.Clients;
using Microsoft.Extensions.Logging;

var loggerFactory = LoggerFactory.Create(builder =>
{
    // Log everything from Algolia in the console, including debug logs
    builder.AddFilter("Algolia", LogLevel.Debug).AddConsole();
});

var client = new SearchClient(
    "ALGOLIA_APPLICATION_ID",
    "ALGOLIA_API_KEY",
    loggerFactory,
);

Timeouts

To change the timeouts for all requests, create a new client configuration, SearchConfig, and set the appropriate options:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

// Create configuration object
var config = new SearchConfig(
    appId: "ALGOLIA_APPLICATION_ID",
    apiKey: "ALGOLIA_API_KEY"
);

// Adjust timeouts
config.ReadTimeout = TimeSpan.FromSeconds(100);
config.WriteTimeout = TimeSpan.FromSeconds(100);
config.ConnectTimeout = TimeSpan.FromSeconds(100);

// Create the client from the configuration
var client = new SearchClient(config);
ConnectTimeout
type: TimeSpan
default: 2

Maximum number of seconds to wait for the connection to be established.

ReadTimeout
type: TimeSpan
default: 5

Maximum number of seconds to wait for a response from the server (for GET requests).

WriteTimeout
type: TimeSpan
default: 30

Maximum number of seconds to wait for a response from the server (for requests other than GET).

User agent information

The API clients send user agent information as User-Agent header. To append your own information to this parameter, use the SearchConfig.UserAgent.AddSegment() method:

1
2
3
4
5
6
7
8
9
using Algolia.Search.Clients;

var config = new SearchConfig(
    "ALGOLIA_APPLICATION_ID",
    "ALGOLIA_API_KEY"
);

config.UserAgent.AddSegment("custom c# client", "optional version");
var client = new SearchClient(config);

This appends “custom c# client (optional version)” to the User-Agent header.

Custom HTTP requesters

You can create your own client for making HTTP requests when initializing the API client, by creating a class that implements IHttpRequester.

1
2
3
4
using Algolia.Search.Clients;

var config = new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");
var client = new SearchClient(config, new CustomRequester());

Headers

To add headers to all requests, use the DefaultHeaders field of the SearchConfig object:

1
2
3
4
5
6
7
8
9
10
11
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var config = new SearchConfig(
    "ALGOLIA_APPLICATION_ID",
    "ALGOLIA_API_KEY"
);

// Add header to all requests
config.DefaultHeaders.Add("extra-header", "greetings");
var client = new SearchClient(config);
Did you find this page helpful?
C# API clients v7