|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Collections.Specialized; |
4 | 4 | using System.Net; |
| 5 | +using System.Text; |
5 | 6 | using System.Threading.Tasks; |
6 | 7 | using Elasticsearch.Net; |
7 | 8 | using FluentAssertions; |
@@ -173,22 +174,90 @@ public void AvailableOptions() |
173 | 174 | */ |
174 | 175 | } |
175 | 176 |
|
176 | | - /** |
177 | | - * You can pass a callback of type `Action<IApiCallDetails>` that can eaves drop every time a response (good or bad) is created. |
178 | | - * If you have complex logging needs this is a good place to add that in. |
| 177 | + /** |
| 178 | + * You can pass a callback of type `Action<IApiCallDetails>` that can eaves drop every time a response (good or bad) is created. |
| 179 | + * If you have complex logging needs this is a good place to add that in. |
| 180 | + */ |
| 181 | + [U] |
| 182 | + public void OnRequestCompletedIsCalled() |
| 183 | + { |
| 184 | + var counter = 0; |
| 185 | + var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
| 186 | + var settings = new ConnectionSettings(connectionPool, new InMemoryConnection()) |
| 187 | + .OnRequestCompleted(r => counter++); |
| 188 | + var client = new ElasticClient(settings); |
| 189 | + client.RootNodeInfo(); |
| 190 | + counter.Should().Be(1); |
| 191 | + client.RootNodeInfoAsync(); |
| 192 | + counter.Should().Be(2); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * An example of using `OnRequestCompleted()` for complex logging. Remember, if you would also like |
| 197 | + * to capture the request and/or response bytes, you also need to set `.DisableDirectStreaming()` |
| 198 | + * to `true` |
179 | 199 | */ |
180 | | - [U]public void OnRequestCompletedIsCalled() |
| 200 | + [U]public async Task UsingOnRequestCompletedForLogging() |
181 | 201 | { |
182 | | - var counter = 0; |
| 202 | + var list = new List<string>(); |
183 | 203 | var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
184 | 204 | var settings = new ConnectionSettings(connectionPool, new InMemoryConnection()) |
185 | | - .OnRequestCompleted(r => counter++); |
| 205 | + .DisableDirectStreaming() |
| 206 | + .OnRequestCompleted(response => |
| 207 | + { |
| 208 | + // log out the request |
| 209 | + if (response.RequestBodyInBytes != null) |
| 210 | + { |
| 211 | + list.Add( |
| 212 | + $"{response.HttpMethod} {response.Uri} \n" + |
| 213 | + $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}"); |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + list.Add($"{response.HttpMethod} {response.Uri}"); |
| 218 | + } |
| 219 | + |
| 220 | + // log out the response |
| 221 | + if (response.ResponseBodyInBytes != null) |
| 222 | + { |
| 223 | + list.Add($"Status: {response.HttpStatusCode}\n" + |
| 224 | + $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" + |
| 225 | + $"{new string('-', 30)}\n"); |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + list.Add($"Status: {response.HttpStatusCode}\n" + |
| 230 | + $"{new string('-', 30)}\n"); |
| 231 | + } |
| 232 | + }); |
| 233 | + |
186 | 234 | var client = new ElasticClient(settings); |
187 | | - client.RootNodeInfo(); |
188 | | - counter.Should().Be(1); |
189 | | - client.RootNodeInfoAsync(); |
190 | | - counter.Should().Be(2); |
191 | | - } |
| 235 | + |
| 236 | + var syncResponse = client.Search<object>(s => s |
| 237 | + .Scroll("2m") |
| 238 | + .Sort(ss => ss |
| 239 | + .Ascending(SortSpecialField.DocumentIndexOrder) |
| 240 | + ) |
| 241 | + ); |
| 242 | + |
| 243 | + list.Count.Should().Be(2); |
| 244 | + |
| 245 | + var asyncResponse = await client.SearchAsync<object>(s => s |
| 246 | + .Scroll("2m") |
| 247 | + .Sort(ss => ss |
| 248 | + .Ascending(SortSpecialField.DocumentIndexOrder) |
| 249 | + ) |
| 250 | + ); |
| 251 | + |
| 252 | + list.Count.Should().Be(4); |
| 253 | + list.ShouldAllBeEquivalentTo(new [] |
| 254 | + { |
| 255 | + "POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}", |
| 256 | + "Status: 200\n------------------------------\n", |
| 257 | + "POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}", |
| 258 | + "Status: 200\n------------------------------\n" |
| 259 | + }); |
| 260 | + } |
192 | 261 |
|
193 | 262 | public void ConfiguringSSL() |
194 | 263 | { |
|
0 commit comments