Skip to content

Commit 764c051

Browse files
committed
ElasticsearchResponse .ToString() will now show the response only when possible
1 parent f7b9549 commit 764c051

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,11 @@ private ElasticsearchResponse<T> WebToElasticsearchResponse<T>(byte[] data, obje
274274
this.SetByteResult(cs as ElasticsearchResponse<byte[]>, bytes);
275275
return cs;
276276
}
277-
cs.ResponseRaw = bytes;
277+
cs.ResponseRaw = _ConnectionSettings.KeepRawResponse ? bytes: null;
278278
}
279279

280280
var result = this._ConnectionSettings.Serializer.Deserialize<T>(cs, s, deserializationState);
281281
cs.Response = result;
282-
cs.ResponseRaw = memoryStream.ToArray();
283282
tracer.SetResult(cs);
284283
return cs;
285284
}
@@ -384,20 +383,20 @@ private IEnumerable<Task> _AsyncSteps<T>(HttpWebRequest request, TaskCompletionS
384383
}
385384
memoryStream.Position = 0;
386385
s = memoryStream;
387-
cs.ResponseRaw = memoryStream.ToArray();
386+
var bytes = memoryStream.ToArray();
388387
if (typeof(T) == typeof(string))
389388
{
390-
this.SetStringResult(cs as ElasticsearchResponse<string>, cs.ResponseRaw);
389+
this.SetStringResult(cs as ElasticsearchResponse<string>, bytes);
391390
SetReturnOnAsycActors(tcs, cs, tracer);
392391
yield break;
393392
}
394393
if (typeof(T) == typeof(byte[]))
395394
{
396-
this.SetByteResult(cs as ElasticsearchResponse<byte[]>, cs.ResponseRaw);
395+
this.SetByteResult(cs as ElasticsearchResponse<byte[]>, bytes);
397396
SetReturnOnAsycActors(tcs, cs, tracer);
398397
yield break;
399398
}
400-
399+
cs.ResponseRaw = _ConnectionSettings.KeepRawResponse ? bytes : null;
401400
}
402401
var t = this._ConnectionSettings.Serializer.DeserializeAsync<T>(cs, s, deserializationState);
403402
yield return t;

src/Elasticsearch.Net/Domain/ElasticsearchResponse.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.CodeDom;
23
using System.Collections.Generic;
34
using System.Configuration;
45
using System.Globalization;
@@ -185,13 +186,21 @@ public override string ToString()
185186
{
186187
var r = this;
187188
var e = r.Error;
189+
string response = "<Response not captured or already read to completion by serializer>";
190+
if (typeof(T) == typeof(string))
191+
response = this.Response as string;
192+
else if (this.Settings.KeepRawResponse)
193+
response = this.ResponseRaw.Utf8String();
194+
else if (typeof(T) == typeof(byte[]))
195+
response = (this.Response as byte[]).Utf8String();
196+
188197
var print = _printFormat.F(
189198
Environment.NewLine,
190199
r.HttpStatusCode.HasValue ? r.HttpStatusCode.Value.ToString(CultureInfo.InvariantCulture) : "-1",
191200
r.RequestMethod,
192201
r.RequestUrl,
193202
r.Request,
194-
"RESPONSE STREAM ALREADY READ BY SERIALIZER"
203+
response
195204
);
196205
if (!this.Success)
197206
{

0 commit comments

Comments
 (0)