< Summary

Information
Class: Ice.Internal.LoggerMiddleware
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/LoggerMiddleware.cs
Tag: 71_18251537082
Line coverage
31%
Covered lines: 26
Uncovered lines: 56
Coverable lines: 82
Total lines: 175
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)

/home/runner/work/ice/ice/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>A middleware that logs warnings for failed dispatches.</summary>
 11internal 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        {
 125            OutgoingResponse response = await _next.dispatchAsync(request).ConfigureAwait(false);
 126            switch (response.replyStatus)
 27            {
 28                case ReplyStatus.Ok:
 29                case ReplyStatus.UserException:
 130                    if (_traceLevel > 0)
 31                    {
 032                        logDispatch(response.replyStatus, request.current);
 33                    }
 034                    break;
 35
 36                case ReplyStatus.UnknownException:
 37                case ReplyStatus.UnknownUserException:
 38                case ReplyStatus.UnknownLocalException:
 039                    logDispatchFailed(response.exceptionDetails, request.current); // always log when middleware install
 040                    break;
 41
 42                default:
 143                    if (_traceLevel > 0 || _warningLevel > 1)
 44                    {
 045                        logDispatchFailed(response.exceptionDetails, request.current);
 46                    }
 47                    break;
 48            }
 149            return response;
 50        }
 151        catch (UserException)
 52        {
 153            if (_traceLevel > 0)
 54            {
 055                logDispatch(ReplyStatus.UserException, request.current);
 56            }
 157            throw;
 58        }
 059        catch (UnknownException ex)
 60        {
 061            logDispatchFailed(ex.ToString(), request.current); // always log when middleware installed
 062            throw;
 63        }
 164        catch (DispatchException ex)
 65        {
 166            if (_traceLevel > 0 || _warningLevel > 1)
 67            {
 068                logDispatchFailed(ex.ToString(), request.current);
 69            }
 170            throw;
 71        }
 072        catch (System.Exception ex)
 73        {
 074            logDispatchFailed(ex.ToString(), request.current);
 075            throw;
 76        }
 177    }
 78
 179    internal LoggerMiddleware(
 180        Object next,
 181        Logger logger,
 182        int traceLevel,
 183        string traceCat,
 184        int warningLevel,
 185        ToStringMode toStringMode)
 86    {
 187        _next = next;
 188        _logger = logger;
 189        _traceLevel = traceLevel;
 190        _traceCat = traceCat;
 191        _warningLevel = warningLevel;
 192        _toStringMode = toStringMode;
 93
 94        Debug.Assert(_traceLevel > 0 || _warningLevel > 0);
 195    }
 96
 97    private void logDispatch(ReplyStatus replyStatus, Current current)
 98    {
 099        var sb = new StringBuilder();
 0100        sb.Append("dispatch of ");
 0101        sb.Append(current.operation);
 0102        sb.Append(" to ");
 0103        printTarget(sb, current);
 0104        sb.Append(" returned a response with reply status ");
 0105        sb.Append(replyStatus);
 106
 0107        _logger.trace(_traceCat, sb.ToString());
 0108    }
 109
 110    private void logDispatchFailed(string? exceptionDetails, Current current)
 111    {
 0112        var sb = new StringBuilder();
 0113        sb.Append("failed to dispatch ");
 0114        sb.Append(current.operation);
 0115        sb.Append(" to ");
 0116        printTarget(sb, current);
 117
 0118        if (exceptionDetails is not null)
 119        {
 0120            sb.Append(":\n");
 0121            sb.Append(exceptionDetails);
 122        }
 123
 0124        _logger.warning(sb.ToString());
 0125    }
 126
 127    private void printTarget(StringBuilder sb, Current current)
 128    {
 0129        sb.Append(Ice.Util.identityToString(current.id, _toStringMode));
 0130        if (current.facet.Length > 0)
 131        {
 0132            sb.Append(" -f ");
 0133            sb.Append(Ice.UtilInternal.StringUtil.escapeString(current.facet, "", _toStringMode));
 134        }
 135
 0136        sb.Append(" over ");
 137
 0138        if (current.con is not null)
 139        {
 0140            ConnectionInfo? connInfo = null;
 141            try
 142            {
 0143                connInfo = current.con.getInfo();
 0144                while (connInfo.underlying is not null)
 145                {
 0146                    connInfo = connInfo.underlying;
 147                }
 0148            }
 0149            catch
 150            {
 151                // Thrown by getInfo() when the connection is closed.
 0152            }
 153
 0154            if (connInfo is IPConnectionInfo ipConnInfo)
 155            {
 0156                sb.Append(ipConnInfo.localAddress);
 0157                sb.Append(':');
 0158                sb.Append(ipConnInfo.localPort);
 0159                sb.Append("<->");
 0160                sb.Append(ipConnInfo.remoteAddress);
 0161                sb.Append(':');
 0162                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.
 0167                sb.Append(current.con.type());
 168            }
 169        }
 170        else
 171        {
 0172            sb.Append("colloc");
 173        }
 0174    }
 175}