|
3 | 3 | using System.ComponentModel; |
4 | 4 | using System.Globalization; |
5 | 5 | using System.Linq; |
| 6 | +using System.Text; |
6 | 7 |
|
7 | 8 | namespace Elasticsearch.Net |
8 | 9 | { |
9 | | - internal static class ResponseStatics |
| 10 | + public static class ResponseStatics |
10 | 11 | { |
11 | | - public static readonly string PrintFormat = "StatusCode: {1}, {0}\tMethod: {2}, {0}\tUrl: {3}, {0}\tRequest: {4}, {0}\tResponse: {5}"; |
12 | | - public static readonly string ErrorFormat = "{0}\tExceptionMessage: {1}{0}\t StackTrace: {2}"; |
13 | | - public static readonly string AlreadyCaptured = "<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>"; |
| 12 | + private static readonly string ResponseAlreadyCaptured = "<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>"; |
| 13 | + private static readonly string RequestAlreadyCaptured = "<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>"; |
| 14 | + public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb) |
| 15 | + { |
| 16 | + sb.AppendLine($"# Audit trail of this API call:"); |
| 17 | + var auditTrail = (r.AuditTrail ?? Enumerable.Empty<Audit>()).ToList(); |
| 18 | + DebugAuditTrail(auditTrail, sb); |
| 19 | + if (r.ServerError != null) sb.AppendLine($"# ServerError: {r.ServerError}"); |
| 20 | + if (r.OriginalException != null) sb.AppendLine($"# OriginalException: {r.OriginalException}"); |
| 21 | + DebugAuditTrailExceptions(auditTrail, sb); |
| 22 | + |
| 23 | + var response = r.ResponseBodyInBytes?.Utf8String() ?? ResponseStatics.ResponseAlreadyCaptured; |
| 24 | + var request = r.RequestBodyInBytes?.Utf8String() ?? ResponseStatics.RequestAlreadyCaptured; |
| 25 | + sb.AppendLine($"# Request:\r\n{request}"); |
| 26 | + sb.AppendLine($"# Response:\r\n{response}"); |
| 27 | + |
| 28 | + return sb.ToString(); |
| 29 | + } |
| 30 | + |
| 31 | + public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuilder sb) |
| 32 | + { |
| 33 | + var auditExceptions = auditTrail.Select((audit, i) => new {audit, i}).Where(a => a.audit.Exception != null); |
| 34 | + foreach (var a in auditExceptions) |
| 35 | + sb.AppendLine($"# Audit exception in step {a.i} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}"); |
| 36 | + } |
| 37 | + |
| 38 | + public static void DebugAuditTrail(List<Audit> auditTrail, StringBuilder sb) |
| 39 | + { |
| 40 | + if (auditTrail == null) return; |
| 41 | + foreach (var audit in auditTrail) |
| 42 | + { |
| 43 | + sb.Append($" - {audit.Event.GetStringValue()}:"); |
| 44 | + if (audit.Node?.Uri != null) sb.Append($" Node: {audit.Node.Uri}"); |
| 45 | + if (audit.Exception != null) sb.Append($" Exception: {audit.Exception.GetType().Name}"); |
| 46 | + sb.AppendLine($" Took: {(audit.Ended - audit.Started)}"); |
| 47 | + } |
| 48 | + } |
14 | 49 | } |
15 | 50 |
|
16 | 51 | public class ElasticsearchResponse<T> : IApiCallDetails |
@@ -59,25 +94,16 @@ public ElasticsearchResponse(int statusCode, IEnumerable<int> allowedStatusCodes |
59 | 94 | this.HttpStatusCode = statusCode; |
60 | 95 | } |
61 | 96 |
|
62 | | - public override string ToString() |
| 97 | + public string DebugInformation |
63 | 98 | { |
64 | | - var r = this; |
65 | | - var e = r.OriginalException; |
66 | | - var response = this.ResponseBodyInBytes?.Utf8String() ?? ResponseStatics.AlreadyCaptured; |
67 | | - |
68 | | - var requestJson = r.RequestBodyInBytes?.Utf8String(); |
69 | | - |
70 | | - var print = string.Format(ResponseStatics.PrintFormat, |
71 | | - Environment.NewLine, |
72 | | - r.HttpStatusCode.HasValue ? r.HttpStatusCode.Value.ToString(CultureInfo.InvariantCulture) : "-1", |
73 | | - r.HttpMethod, |
74 | | - r.Uri, |
75 | | - requestJson, |
76 | | - response |
77 | | - ); |
78 | | - if (!this.Success && e != null) |
79 | | - print += string.Format(ResponseStatics.ErrorFormat,Environment.NewLine, e.Message, e.StackTrace); |
80 | | - return print; |
| 99 | + get |
| 100 | + { |
| 101 | + var sb = new StringBuilder(); |
| 102 | + sb.AppendLine(this.ToString()); |
| 103 | + return ResponseStatics.DebugInformationBuilder(this, sb); |
| 104 | + } |
81 | 105 | } |
| 106 | + |
| 107 | + public override string ToString() => $"{(Success ? "S" : "Uns")}uccesful low level call on {HttpMethod.GetStringValue()}: {Uri.PathAndQuery}"; |
82 | 108 | } |
83 | 109 | } |
0 commit comments