| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using CoreWCF.Runtime.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Runtime |
| | | 10 | | { |
| | | 11 | | internal class ExceptionTrace |
| | | 12 | | { |
| | | 13 | | private readonly string _eventSourceName; |
| | | 14 | | private readonly EtwDiagnosticTrace _diagnosticTrace; |
| | | 15 | | |
| | 1 | 16 | | internal ExceptionTrace(string eventSourceName, EtwDiagnosticTrace diagnosticTrace) |
| | | 17 | | { |
| | | 18 | | Fx.Assert(diagnosticTrace != null, "'diagnosticTrace' MUST NOT be NULL."); |
| | | 19 | | |
| | 1 | 20 | | _eventSourceName = eventSourceName; |
| | 1 | 21 | | _diagnosticTrace = diagnosticTrace; |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | public void AsWarning(Exception exception) |
| | | 25 | | { |
| | | 26 | | //Traces a warning trace message |
| | | 27 | | //TraceCore.HandledExceptionWarning(this.diagnosticTrace, exception != null ? exception.ToString() : string. |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public Exception AsError(Exception exception) |
| | | 31 | | { |
| | | 32 | | // AggregateExceptions are automatically unwrapped. |
| | 0 | 33 | | if (exception is AggregateException aggregateException) |
| | | 34 | | { |
| | 0 | 35 | | return AsError<Exception>(aggregateException); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // TargetInvocationExceptions are automatically unwrapped. |
| | 0 | 39 | | if (exception is TargetInvocationException targetInvocationException && targetInvocationException.InnerExcep |
| | | 40 | | { |
| | 0 | 41 | | return AsError(targetInvocationException.InnerException); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | return TraceException<Exception>(exception); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public Exception AsError<TPreferredException>(AggregateException aggregateException) |
| | | 48 | | { |
| | 0 | 49 | | 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. |
| | 0 | 58 | | if (Fx.IsFatal(aggregateException)) |
| | | 59 | | { |
| | 0 | 60 | | 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. |
| | 0 | 65 | | System.Collections.ObjectModel.ReadOnlyCollection<Exception> innerExceptions = aggregateException.Flatten(). |
| | 0 | 66 | | if (innerExceptions.Count == 0) |
| | | 67 | | { |
| | 0 | 68 | | return TraceException(aggregateException, eventSource); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Find the first inner exception, giving precedence to TPreferredException |
| | 0 | 72 | | Exception favoredException = null; |
| | 0 | 73 | | foreach (Exception nextInnerException in innerExceptions) |
| | | 74 | | { |
| | | 75 | | // AggregateException may wrap TargetInvocationException, so unwrap those as well |
| | | 76 | | |
| | 0 | 77 | | Exception innerException = (nextInnerException is TargetInvocationException targetInvocationException && |
| | 0 | 78 | | ? targetInvocationException.InnerException |
| | 0 | 79 | | : nextInnerException; |
| | | 80 | | |
| | 0 | 81 | | if (innerException is TPreferredException && favoredException == null) |
| | | 82 | | { |
| | 0 | 83 | | favoredException = innerException; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // All inner exceptions are traced |
| | 0 | 87 | | TraceException<Exception>(innerException, eventSource); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if (favoredException == null) |
| | | 91 | | { |
| | | 92 | | Fx.Assert(innerExceptions.Count > 0, "InnerException.Count is known to be > 0 here."); |
| | 0 | 93 | | favoredException = innerExceptions[0]; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return favoredException; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public ArgumentException Argument(string paramName, string message) |
| | | 100 | | { |
| | 0 | 101 | | return TraceException(new ArgumentException(message, paramName)); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | public ArgumentNullException ArgumentNull(string paramName) |
| | | 105 | | { |
| | 0 | 106 | | return TraceException(new ArgumentNullException(paramName)); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public ArgumentNullException ArgumentNull(string paramName, string message) |
| | | 110 | | { |
| | 0 | 111 | | return TraceException(new ArgumentNullException(paramName, message)); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private TException TraceException<TException>(TException exception) |
| | | 115 | | where TException : Exception |
| | | 116 | | { |
| | 0 | 117 | | 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 | | |
| | 0 | 130 | | return exception; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | public ArgumentOutOfRangeException ArgumentOutOfRange(string paramName, object actualValue, string message) |
| | | 134 | | { |
| | 0 | 135 | | 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 |
| | 0 | 146 | | 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 |
| | 0 | 152 | | } |
| | | 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 | | //} |
| | 6 | 183 | | } |
| | | 184 | | } |
| | | 185 | | } |