| | | 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.Runtime.CompilerServices; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF |
| | | 11 | | { |
| | | 12 | | internal class DiagnosticUtility |
| | | 13 | | { |
| | | 14 | | private static ExceptionUtility s_exceptionUtility = (ExceptionUtility)null; |
| | | 15 | | private static readonly object s_lockObject = new object(); |
| | | 16 | | |
| | | 17 | | internal static ExceptionUtility ExceptionUtility |
| | | 18 | | { |
| | | 19 | | get |
| | | 20 | | { |
| | | 21 | | return s_exceptionUtility ?? GetExceptionUtility(); |
| | | 22 | | } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | private static ExceptionUtility GetExceptionUtility() |
| | | 26 | | { |
| | | 27 | | lock (s_lockObject) |
| | | 28 | | { |
| | | 29 | | if (s_exceptionUtility == null) |
| | | 30 | | { |
| | | 31 | | // TODO: Make this generic shared code used by multiple assemblies |
| | | 32 | | //exceptionUtility = new ExceptionUtility("System.ServiceModel", "System.ServiceModel 4.0.0.0", (obj |
| | | 33 | | s_exceptionUtility = new ExceptionUtility(); |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | return s_exceptionUtility; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | internal static void TraceHandledException(Exception exception, TraceEventType traceEventType) |
| | | 41 | | { |
| | | 42 | | Fx.Exception.TraceHandledException(exception, traceEventType); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | [Conditional("DEBUG")] |
| | | 46 | | internal static void DebugAssert(bool condition, string message) |
| | | 47 | | { |
| | | 48 | | if (!condition) |
| | | 49 | | { |
| | | 50 | | DebugAssert(message); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 55 | | [Conditional("DEBUG")] |
| | | 56 | | internal static void DebugAssert(string message) |
| | | 57 | | { |
| | | 58 | | Fx.Assert(message); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 62 | | internal static Exception FailFast(string message) |
| | | 63 | | { |
| | | 64 | | try |
| | | 65 | | { |
| | | 66 | | try |
| | | 67 | | { |
| | | 68 | | ExceptionUtility.TraceFailFast(message); |
| | | 69 | | } |
| | | 70 | | finally |
| | | 71 | | { |
| | | 72 | | Environment.FailFast(message); |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | catch |
| | | 76 | | { |
| | | 77 | | } |
| | | 78 | | Environment.FailFast(message); |
| | | 79 | | return (Exception)null; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | internal class ExceptionUtility |
| | | 84 | | { |
| | | 85 | | internal ArgumentException ThrowHelperArgument(string message) |
| | | 86 | | { |
| | 0 | 87 | | return (ArgumentException)ThrowHelperError(new ArgumentException(message)); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal ArgumentException ThrowHelperArgument(string paramName, string message) |
| | | 91 | | { |
| | 0 | 92 | | return (ArgumentException)ThrowHelperError(new ArgumentException(message, paramName)); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | internal ArgumentNullException ThrowHelperArgumentNull(string paramName) |
| | | 96 | | { |
| | 0 | 97 | | return (ArgumentNullException)ThrowHelperError(new ArgumentNullException(paramName)); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | internal Exception ThrowHelperFatal(string message, Exception innerException) |
| | | 101 | | { |
| | 0 | 102 | | return ThrowHelperError(new FatalException(message, innerException)); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | internal Exception ThrowHelperError(Exception exception) |
| | | 106 | | { |
| | 0 | 107 | | return ThrowHelper(exception, TraceEventType.Error); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | internal Exception ThrowHelperWarning(Exception exception) |
| | | 111 | | { |
| | 0 | 112 | | return ThrowHelper(exception, TraceEventType.Warning); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | internal Exception ThrowHelper(Exception exception, TraceEventType eventType) |
| | | 116 | | { |
| | 0 | 117 | | return ThrowHelper(exception, eventType, null); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | internal Exception ThrowHelper(Exception exception, TraceEventType eventType, TraceRecord extendedData) |
| | | 121 | | { |
| | | 122 | | //if ((_diagnosticTrace == null ? 0 : (_diagnosticTrace.ShouldTrace(eventType) ? 1 : 0)) != 0) |
| | | 123 | | //{ |
| | | 124 | | // using ( |
| | | 125 | | // ExceptionUtility.useStaticActivityId |
| | | 126 | | // ? Activity.CreateActivity(ExceptionUtility.activityId) |
| | | 127 | | // : (Activity)null) |
| | | 128 | | // _diagnosticTrace.TraceEvent(eventType, 131075, |
| | | 129 | | // LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", |
| | | 130 | | // "ThrowingException"), TraceSRCommon.Format("ThrowingException"), extendedData, exception, |
| | | 131 | | // (object)null); |
| | | 132 | | // IDictionary data = exception.Data; |
| | | 133 | | // if (data != null && !data.IsReadOnly && !data.IsFixedSize) |
| | | 134 | | // { |
| | | 135 | | // object obj = |
| | | 136 | | // data[(object)"System.ServiceModel.Diagnostics.ExceptionUtility.ExceptionStackAsString"]; |
| | | 137 | | // string str1 = obj == null ? "" : obj as string; |
| | | 138 | | // if (str1 != null) |
| | | 139 | | // { |
| | | 140 | | // string stackTrace = exception.StackTrace; |
| | | 141 | | // if (!string.IsNullOrEmpty(stackTrace)) |
| | | 142 | | // { |
| | | 143 | | // string str2 = str1 + (str1.Length == 0 ? "" : Environment.NewLine) + "throw" + |
| | | 144 | | // Environment.NewLine + stackTrace + Environment.NewLine + "catch" + |
| | | 145 | | // Environment.NewLine; |
| | | 146 | | // data[(object)"System.ServiceModel.Diagnostics.ExceptionUtility.ExceptionStackAsString"] |
| | | 147 | | // = (object)str2; |
| | | 148 | | // } |
| | | 149 | | // } |
| | | 150 | | // } |
| | | 151 | | //} |
| | | 152 | | //this.exceptionTrace.TraceEtwException(exception, eventType); |
| | 0 | 153 | | return exception; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public Exception ThrowHelperXml(XmlReader reader, string message) |
| | | 157 | | { |
| | 0 | 158 | | return ThrowHelperXml(reader, message, null); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public Exception ThrowHelperXml(XmlReader reader, string message, Exception inner) |
| | | 162 | | { |
| | 0 | 163 | | IXmlLineInfo lineInfo = reader as IXmlLineInfo; |
| | 0 | 164 | | return ThrowHelperError(new XmlException( |
| | 0 | 165 | | message, |
| | 0 | 166 | | inner, |
| | 0 | 167 | | (null != lineInfo) ? lineInfo.LineNumber : 0, |
| | 0 | 168 | | (null != lineInfo) ? lineInfo.LinePosition : 0)); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal Exception ThrowHelperCallback(Exception innerException) |
| | | 172 | | { |
| | 0 | 173 | | return ThrowHelperCallback(TraceSR.GenericCallbackException, innerException); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal Exception ThrowHelperCallback(string message, Exception innerException) |
| | | 177 | | { |
| | 0 | 178 | | return ThrowHelperCritical(new CallbackException(message, innerException)); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | internal Exception ThrowHelperCritical(Exception exception) |
| | | 182 | | { |
| | 0 | 183 | | return ThrowHelper(exception, TraceEventType.Critical); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | internal class TraceRecord |
| | | 187 | | { |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 191 | | internal void TraceFailFast(string message) |
| | | 192 | | { |
| | | 193 | | //Microsoft.Runtime.Diagnostics.EventLogger logger = null; |
| | | 194 | | //try |
| | | 195 | | //{ |
| | | 196 | | // logger = new Microsoft.Runtime.Diagnostics.EventLogger(this.eventSourceName, this.diagnosticTrace); |
| | | 197 | | //} |
| | | 198 | | //finally |
| | | 199 | | //{ |
| | | 200 | | // TraceFailFast(message, logger); |
| | | 201 | | //} |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | internal Exception ThrowHelperArgumentNull(string paramName, string message) |
| | | 205 | | { |
| | 0 | 206 | | return (ArgumentNullException)ThrowHelperError(new ArgumentNullException(paramName, message)); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | public Exception ThrowHelperInvalidOperation(string message) |
| | | 210 | | { |
| | 0 | 211 | | return ThrowHelperError(new InvalidOperationException(message)); |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | } |