< Summary

Information
Class: Ice.Internal.LoggerMiddleware
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/LoggerMiddleware.cs
Tag: 91_21789722663
Line coverage
31%
Covered lines: 26
Uncovered lines: 56
Coverable lines: 82
Total lines: 177
Line coverage: 31.7%
Branch coverage
34%
Covered branches: 9
Total branches: 26
Branch coverage: 34.6%
Method coverage
40%
Covered methods: 2
Total methods: 5
Method coverage: 40%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
dispatchAsync()56.25%521648%
.ctor(...)100%11100%
logDispatch(...)100%210%
logDispatchFailed(...)0%620%
printTarget(...)0%7280%

File(s)

/_/csharp/src/Ice/Internal/LoggerMiddleware.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3#nullable enable
 4
 5using System.Diagnostics;
 6using System.Text;
 7
 8namespace Ice.Internal;
 9
 10/// <summary>
 11/// A middleware that logs warnings for failed dispatches.
 12/// </summary>
 13internal sealed class LoggerMiddleware : Object
 14{
 15    private readonly Object _next;
 16    private readonly Logger _logger;
 17
 18    private readonly string _traceCat;
 19    private readonly int _traceLevel;
 20    private readonly int _warningLevel;
 21    private readonly ToStringMode _toStringMode;
 22
 23    public async ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request)
 24    {
 25        try
 26        {
 127            OutgoingResponse response = await _next.dispatchAsync(request).ConfigureAwait(false);
 128            switch (response.replyStatus)
 29            {
 30                case ReplyStatus.Ok:
 31                case ReplyStatus.UserException:
 132                    if (_traceLevel > 0)
 33                    {
 034                        logDispatch(response.replyStatus, request.current);
 35                    }
 036                    break;
 37
 38                case ReplyStatus.UnknownException:
 39                case ReplyStatus.UnknownUserException:
 40                case ReplyStatus.UnknownLocalException:
 041                    logDispatchFailed(response.exceptionDetails, request.current); // always log when middleware install
 042                    break;
 43
 44                default:
 145                    if (_traceLevel > 0 || _warningLevel > 1)
 46                    {
 047                        logDispatchFailed(response.exceptionDetails, request.current);
 48                    }
 49                    break;
 50            }
 151            return response;
 52        }
 153        catch (UserException)
 54        {
 155            if (_traceLevel > 0)
 56            {
 057                logDispatch(ReplyStatus.UserException, request.current);
 58            }
 159            throw;
 60        }
 061        catch (UnknownException ex)
 62        {
 063            logDispatchFailed(ex.ToString(), request.current); // always log when middleware installed
 064            throw;
 65        }
 166        catch (DispatchException ex)
 67        {
 168            if (_traceLevel > 0 || _warningLevel > 1)
 69            {
 070                logDispatchFailed(ex.ToString(), request.current);
 71            }
 172            throw;
 73        }
 074        catch (System.Exception ex)
 75        {
 076            logDispatchFailed(ex.ToString(), request.current);
 077            throw;
 78        }
 179    }
 80
 181    internal LoggerMiddleware(
 182        Object next,
 183        Logger logger,
 184        int traceLevel,
 185        string traceCat,
 186        int warningLevel,
 187        ToStringMode toStringMode)
 88    {
 189        _next = next;
 190        _logger = logger;
 191        _traceLevel = traceLevel;
 192        _traceCat = traceCat;
 193        _warningLevel = warningLevel;
 194        _toStringMode = toStringMode;
 95
 96        Debug.Assert(_traceLevel > 0 || _warningLevel > 0);
 197    }
 98
 99    private void logDispatch(ReplyStatus replyStatus, Current current)
 100    {
 0101        var sb = new StringBuilder();
 0102        sb.Append("dispatch of ");
 0103        sb.Append(current.operation);
 0104        sb.Append(" to ");
 0105        printTarget(sb, current);
 0106        sb.Append(" returned a response with reply status ");
 0107        sb.Append(replyStatus);
 108
 0109        _logger.trace(_traceCat, sb.ToString());
 0110    }
 111
 112    private void logDispatchFailed(string? exceptionDetails, Current current)
 113    {
 0114        var sb = new StringBuilder();
 0115        sb.Append("failed to dispatch ");
 0116        sb.Append(current.operation);
 0117        sb.Append(" to ");
 0118        printTarget(sb, current);
 119
 0120        if (exceptionDetails is not null)
 121        {
 0122            sb.Append(":\n");
 0123            sb.Append(exceptionDetails);
 124        }
 125
 0126        _logger.warning(sb.ToString());
 0127    }
 128
 129    private void printTarget(StringBuilder sb, Current current)
 130    {
 0131        sb.Append(Ice.Util.identityToString(current.id, _toStringMode));
 0132        if (current.facet.Length > 0)
 133        {
 0134            sb.Append(" -f ");
 0135            sb.Append(Ice.UtilInternal.StringUtil.escapeString(current.facet, "", _toStringMode));
 136        }
 137
 0138        sb.Append(" over ");
 139
 0140        if (current.con is not null)
 141        {
 0142            ConnectionInfo? connInfo = null;
 143            try
 144            {
 0145                connInfo = current.con.getInfo();
 0146                while (connInfo.underlying is not null)
 147                {
 0148                    connInfo = connInfo.underlying;
 149                }
 0150            }
 0151            catch
 152            {
 153                // Thrown by getInfo() when the connection is closed.
 0154            }
 155
 0156            if (connInfo is IPConnectionInfo ipConnInfo)
 157            {
 0158                sb.Append(ipConnInfo.localAddress);
 0159                sb.Append(':');
 0160                sb.Append(ipConnInfo.localPort);
 0161                sb.Append("<->");
 0162                sb.Append(ipConnInfo.remoteAddress);
 0163                sb.Append(':');
 0164                sb.Append(ipConnInfo.remotePort);
 165            }
 166            else
 167            {
 168                // Connection.ToString() returns a multiline string, so we just use type here for bt and similar.
 0169                sb.Append(current.con.type());
 170            }
 171        }
 172        else
 173        {
 0174            sb.Append("colloc");
 175        }
 0176    }
 177}