For debugging purposes it can be useful to see the executed REST call. To make this possible using NEST you first have to disable Direct Streaming after which you can start capturing request and response data.
Here is a short snippet on how to enable this in your application:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
connectionSettings | |
.DisableDirectStreaming()//We do this to be able to capture the request and responses(don't do this in production!!!!) | |
.OnRequestCompleted(apiCallDetails => | |
{ | |
// log out the request and the request body, if one exists for the type of request | |
if (apiCallDetails.RequestBodyInBytes != null) | |
{ | |
Trace.Write( | |
$"{apiCallDetails.HttpMethod} {apiCallDetails.Uri} " + | |
$"{Encoding.UTF8.GetString(apiCallDetails.RequestBodyInBytes)}"); | |
} | |
else | |
{ | |
Trace.Write($"{apiCallDetails.HttpMethod} {apiCallDetails.Uri}"); | |
} | |
// log out the response and the response body, if one exists for the type of response | |
if (apiCallDetails.ResponseBodyInBytes != null) | |
{ | |
Trace.Write($"Status: {apiCallDetails.HttpStatusCode}" + | |
$"{Encoding.UTF8.GetString(apiCallDetails.ResponseBodyInBytes)}"); | |
} | |
else | |
{ | |
Trace.Write($"Status: {apiCallDetails.HttpStatusCode}"); | |
} | |
}); | |
var client = new ElasticClient(connectionSettings); |
Important: Don’t forget to disable this when you go to production, it has a big impact on performance.