< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Runtime.ExceptionTrace
Assembly: CoreWCF.NetNamedPipe
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Runtime/ExceptionTrace.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 185
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
AsWarning(...)100%110%
AsError(...)0%660%
AsError(...)100%110%
AsError(...)0%16160%
Argument(...)100%110%
ArgumentNull(...)100%110%
ArgumentNull(...)100%110%
TraceException(...)100%110%
TraceException(...)100%110%
ArgumentOutOfRange(...)100%110%
ObjectDisposed(...)100%110%
TraceUnhandledException(...)100%110%
TraceHandledException(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/src/CoreWCF/Runtime/ExceptionTrace.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Reflection;
 7using CoreWCF.Runtime.Diagnostics;
 8
 9namespace CoreWCF.Runtime
 10{
 11    internal class ExceptionTrace
 12    {
 13        private readonly string _eventSourceName;
 14        private readonly EtwDiagnosticTrace _diagnosticTrace;
 15
 016        internal ExceptionTrace(string eventSourceName, EtwDiagnosticTrace diagnosticTrace)
 17        {
 18            Fx.Assert(diagnosticTrace != null, "'diagnosticTrace' MUST NOT be NULL.");
 19
 020            _eventSourceName = eventSourceName;
 021            _diagnosticTrace = diagnosticTrace;
 022        }
 23
 24        public void AsWarning(Exception exception)
 25        {
 26            //Traces a warning trace message
 27            //TraceCore.HandledExceptionWarning(this.diagnosticTrace, exception != null ? exception.ToString() : string.
 028        }
 29
 30        public Exception AsError(Exception exception)
 31        {
 32            // AggregateExceptions are automatically unwrapped.
 033            if (exception is AggregateException aggregateException)
 34            {
 035                return AsError<Exception>(aggregateException);
 36            }
 37
 38            // TargetInvocationExceptions are automatically unwrapped.
 039            if (exception is TargetInvocationException targetInvocationException && targetInvocationException.InnerExcep
 40            {
 041                return AsError(targetInvocationException.InnerException);
 42            }
 43
 044            return TraceException<Exception>(exception);
 45        }
 46
 47        public Exception AsError<TPreferredException>(AggregateException aggregateException)
 48        {
 049            return AsError<TPreferredException>(aggregateException, _eventSourceName);
 50        }
 51
 52        public Exception AsError<TPreferredException>(AggregateException aggregateException, string eventSource)
 53        {
 54            Fx.Assert(aggregateException != null, "aggregateException cannot be null.");
 55
 56            // If aggregateException contains any fatal exceptions, return it directly
 57            // without tracing it or any inner exceptions.
 058            if (Fx.IsFatal(aggregateException))
 59            {
 060                return aggregateException;
 61            }
 62
 63            // Collapse possibly nested graph into a flat list.
 64            // Empty inner exception list is unlikely but possible via public api.
 065            System.Collections.ObjectModel.ReadOnlyCollection<Exception> innerExceptions = aggregateException.Flatten().
 066            if (innerExceptions.Count == 0)
 67            {
 068                return TraceException(aggregateException, eventSource);
 69            }
 70
 71            // Find the first inner exception, giving precedence to TPreferredException
 072            Exception favoredException = null;
 073            foreach (Exception nextInnerException in innerExceptions)
 74            {
 75                // AggregateException may wrap TargetInvocationException, so unwrap those as well
 76
 077                Exception innerException = (nextInnerException is TargetInvocationException targetInvocationException &&
 078                                                ? targetInvocationException.InnerException
 079                                                : nextInnerException;
 80
 081                if (innerException is TPreferredException && favoredException == null)
 82                {
 083                    favoredException = innerException;
 84                }
 85
 86                // All inner exceptions are traced
 087                TraceException<Exception>(innerException, eventSource);
 88            }
 89
 090            if (favoredException == null)
 91            {
 92                Fx.Assert(innerExceptions.Count > 0, "InnerException.Count is known to be > 0 here.");
 093                favoredException = innerExceptions[0];
 94            }
 95
 096            return favoredException;
 97        }
 98
 99        public ArgumentException Argument(string paramName, string message)
 100        {
 0101            return TraceException(new ArgumentException(message, paramName));
 102        }
 103
 104        public ArgumentNullException ArgumentNull(string paramName)
 105        {
 0106            return TraceException(new ArgumentNullException(paramName));
 107        }
 108
 109        public ArgumentNullException ArgumentNull(string paramName, string message)
 110        {
 0111            return TraceException(new ArgumentNullException(paramName, message));
 112        }
 113
 114        private TException TraceException<TException>(TException exception)
 115            where TException : Exception
 116        {
 0117            return TraceException<TException>(exception, _eventSourceName);
 118        }
 119
 120        private TException TraceException<TException>(TException exception, string eventSource)
 121    where TException : Exception
 122        {
 123            //if (TraceCore.ThrowingExceptionIsEnabled(this.diagnosticTrace))
 124            //{
 125            //    TraceCore.ThrowingException(this.diagnosticTrace, eventSource, exception != null ? exception.ToString(
 126            //}
 127
 128            //BreakOnException(exception);
 129
 0130            return exception;
 131        }
 132
 133        public ArgumentOutOfRangeException ArgumentOutOfRange(string paramName, object actualValue, string message)
 134        {
 0135            return TraceException(new ArgumentOutOfRangeException(paramName, actualValue, message));
 136        }
 137
 138        // When throwing ObjectDisposedException, it is highly recommended that you use this ctor
 139        // [C#]
 140        // public ObjectDisposedException(string objectName, string message);
 141        // And provide null for objectName but meaningful and relevant message for message.
 142        // It is recommended because end user really does not care or can do anything on the disposed object, commonly a
 143        public ObjectDisposedException ObjectDisposed(string message)
 144        {
 145            // pass in null, not disposedObject.GetType().FullName as per the above guideline
 0146            return TraceException(new ObjectDisposedException(null, message));
 147        }
 148
 149        public void TraceUnhandledException(Exception exception)
 150        {
 151            //TraceCore.UnhandledException(this.diagnosticTrace, exception != null ? exception.ToString() : string.Empty
 0152        }
 153
 154        public void TraceHandledException(Exception exception, TraceEventType traceEventType)
 155        {
 156            //switch (traceEventType)
 157            //{
 158            //    case TraceEventType.Error:
 159            //        if (TraceCore.HandledExceptionErrorIsEnabled(this.diagnosticTrace))
 160            //        {
 161            //            TraceCore.HandledExceptionError(this.diagnosticTrace, exception != null ? exception.ToString()
 162            //        }
 163            //        break;
 164            //    case TraceEventType.Warning:
 165            //        if (TraceCore.HandledExceptionWarningIsEnabled(this.diagnosticTrace))
 166            //        {
 167            //            TraceCore.HandledExceptionWarning(this.diagnosticTrace, exception != null ? exception.ToString
 168            //        }
 169            //        break;
 170            //    case TraceEventType.Verbose:
 171            //        if (TraceCore.HandledExceptionVerboseIsEnabled(this.diagnosticTrace))
 172            //        {
 173            //            TraceCore.HandledExceptionVerbose(this.diagnosticTrace, exception != null ? exception.ToString
 174            //        }
 175            //        break;
 176            //    default:
 177            //        if (TraceCore.HandledExceptionIsEnabled(this.diagnosticTrace))
 178            //        {
 179            //            TraceCore.HandledException(this.diagnosticTrace, exception != null ? exception.ToString() : st
 180            //        }
 181            //        break;
 182            //}
 0183        }
 184    }
 185}