| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Text; |
| | 7 | |
|
| | 8 | | namespace Ice.Internal; |
| | 9 | |
|
| | 10 | | /// <summary>A middleware that logs warnings for failed dispatches.</summary> |
| | 11 | | internal sealed class LoggerMiddleware : Object |
| | 12 | | { |
| | 13 | | private readonly Object _next; |
| | 14 | | private readonly Logger _logger; |
| | 15 | |
|
| | 16 | | private readonly string _traceCat; |
| | 17 | | private readonly int _traceLevel; |
| | 18 | | private readonly int _warningLevel; |
| | 19 | | private readonly ToStringMode _toStringMode; |
| | 20 | |
|
| | 21 | | public async ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) |
| | 22 | | { |
| | 23 | | try |
| | 24 | | { |
| 1 | 25 | | OutgoingResponse response = await _next.dispatchAsync(request).ConfigureAwait(false); |
| 1 | 26 | | switch (response.replyStatus) |
| | 27 | | { |
| | 28 | | case ReplyStatus.Ok: |
| | 29 | | case ReplyStatus.UserException: |
| 1 | 30 | | if (_traceLevel > 0) |
| | 31 | | { |
| 0 | 32 | | logDispatch(response.replyStatus, request.current); |
| | 33 | | } |
| 0 | 34 | | break; |
| | 35 | |
|
| | 36 | | case ReplyStatus.UnknownException: |
| | 37 | | case ReplyStatus.UnknownUserException: |
| | 38 | | case ReplyStatus.UnknownLocalException: |
| 0 | 39 | | logDispatchFailed(response.exceptionDetails, request.current); // always log when middleware install |
| 0 | 40 | | break; |
| | 41 | |
|
| | 42 | | default: |
| 1 | 43 | | if (_traceLevel > 0 || _warningLevel > 1) |
| | 44 | | { |
| 0 | 45 | | logDispatchFailed(response.exceptionDetails, request.current); |
| | 46 | | } |
| | 47 | | break; |
| | 48 | | } |
| 1 | 49 | | return response; |
| | 50 | | } |
| 1 | 51 | | catch (UserException) |
| | 52 | | { |
| 1 | 53 | | if (_traceLevel > 0) |
| | 54 | | { |
| 0 | 55 | | logDispatch(ReplyStatus.UserException, request.current); |
| | 56 | | } |
| 1 | 57 | | throw; |
| | 58 | | } |
| 0 | 59 | | catch (UnknownException ex) |
| | 60 | | { |
| 0 | 61 | | logDispatchFailed(ex.ToString(), request.current); // always log when middleware installed |
| 0 | 62 | | throw; |
| | 63 | | } |
| 1 | 64 | | catch (DispatchException ex) |
| | 65 | | { |
| 1 | 66 | | if (_traceLevel > 0 || _warningLevel > 1) |
| | 67 | | { |
| 0 | 68 | | logDispatchFailed(ex.ToString(), request.current); |
| | 69 | | } |
| 1 | 70 | | throw; |
| | 71 | | } |
| 0 | 72 | | catch (System.Exception ex) |
| | 73 | | { |
| 0 | 74 | | logDispatchFailed(ex.ToString(), request.current); |
| 0 | 75 | | throw; |
| | 76 | | } |
| 1 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | internal LoggerMiddleware( |
| 1 | 80 | | Object next, |
| 1 | 81 | | Logger logger, |
| 1 | 82 | | int traceLevel, |
| 1 | 83 | | string traceCat, |
| 1 | 84 | | int warningLevel, |
| 1 | 85 | | ToStringMode toStringMode) |
| | 86 | | { |
| 1 | 87 | | _next = next; |
| 1 | 88 | | _logger = logger; |
| 1 | 89 | | _traceLevel = traceLevel; |
| 1 | 90 | | _traceCat = traceCat; |
| 1 | 91 | | _warningLevel = warningLevel; |
| 1 | 92 | | _toStringMode = toStringMode; |
| | 93 | |
|
| | 94 | | Debug.Assert(_traceLevel > 0 || _warningLevel > 0); |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | private void logDispatch(ReplyStatus replyStatus, Current current) |
| | 98 | | { |
| 0 | 99 | | var sb = new StringBuilder(); |
| 0 | 100 | | sb.Append("dispatch of "); |
| 0 | 101 | | sb.Append(current.operation); |
| 0 | 102 | | sb.Append(" to "); |
| 0 | 103 | | printTarget(sb, current); |
| 0 | 104 | | sb.Append(" returned a response with reply status "); |
| 0 | 105 | | sb.Append(replyStatus); |
| | 106 | |
|
| 0 | 107 | | _logger.trace(_traceCat, sb.ToString()); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private void logDispatchFailed(string? exceptionDetails, Current current) |
| | 111 | | { |
| 0 | 112 | | var sb = new StringBuilder(); |
| 0 | 113 | | sb.Append("failed to dispatch "); |
| 0 | 114 | | sb.Append(current.operation); |
| 0 | 115 | | sb.Append(" to "); |
| 0 | 116 | | printTarget(sb, current); |
| | 117 | |
|
| 0 | 118 | | if (exceptionDetails is not null) |
| | 119 | | { |
| 0 | 120 | | sb.Append(":\n"); |
| 0 | 121 | | sb.Append(exceptionDetails); |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | _logger.warning(sb.ToString()); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | private void printTarget(StringBuilder sb, Current current) |
| | 128 | | { |
| 0 | 129 | | sb.Append(Ice.Util.identityToString(current.id, _toStringMode)); |
| 0 | 130 | | if (current.facet.Length > 0) |
| | 131 | | { |
| 0 | 132 | | sb.Append(" -f "); |
| 0 | 133 | | sb.Append(Ice.UtilInternal.StringUtil.escapeString(current.facet, "", _toStringMode)); |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | sb.Append(" over "); |
| | 137 | |
|
| 0 | 138 | | if (current.con is not null) |
| | 139 | | { |
| 0 | 140 | | ConnectionInfo? connInfo = null; |
| | 141 | | try |
| | 142 | | { |
| 0 | 143 | | connInfo = current.con.getInfo(); |
| 0 | 144 | | while (connInfo.underlying is not null) |
| | 145 | | { |
| 0 | 146 | | connInfo = connInfo.underlying; |
| | 147 | | } |
| 0 | 148 | | } |
| 0 | 149 | | catch |
| | 150 | | { |
| | 151 | | // Thrown by getInfo() when the connection is closed. |
| 0 | 152 | | } |
| | 153 | |
|
| 0 | 154 | | if (connInfo is IPConnectionInfo ipConnInfo) |
| | 155 | | { |
| 0 | 156 | | sb.Append(ipConnInfo.localAddress); |
| 0 | 157 | | sb.Append(':'); |
| 0 | 158 | | sb.Append(ipConnInfo.localPort); |
| 0 | 159 | | sb.Append("<->"); |
| 0 | 160 | | sb.Append(ipConnInfo.remoteAddress); |
| 0 | 161 | | sb.Append(':'); |
| 0 | 162 | | sb.Append(ipConnInfo.remotePort); |
| | 163 | | } |
| | 164 | | else |
| | 165 | | { |
| | 166 | | // Connection.ToString() returns a multiline string, so we just use type here for bt and similar. |
| 0 | 167 | | sb.Append(current.con.type()); |
| | 168 | | } |
| | 169 | | } |
| | 170 | | else |
| | 171 | | { |
| 0 | 172 | | sb.Append("colloc"); |
| | 173 | | } |
| 0 | 174 | | } |
| | 175 | | } |