1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Diagnostics
{
using System;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.ConstrainedExecution;
using System.Runtime.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Runtime.Serialization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Collections;
using System.Xml;
class ExceptionUtility
{
const string ExceptionStackAsStringKey = "System.ServiceModel.Diagnostics.ExceptionUtility.ExceptionStackAsString";
// This field should be only used for debug build.
internal static ExceptionUtility mainInstance;
LegacyDiagnosticTrace diagnosticTrace;
ExceptionTrace exceptionTrace;
string name;
string eventSourceName;
[ThreadStatic]
static Guid activityId;
[ThreadStatic]
static bool useStaticActivityId;
[Obsolete("For SMDiagnostics.dll use only. Call DiagnosticUtility.ExceptionUtility instead")]
internal ExceptionUtility(string name, string eventSourceName, object diagnosticTrace, object exceptionTrace)
{
this.diagnosticTrace = (LegacyDiagnosticTrace)diagnosticTrace;
this.exceptionTrace = (ExceptionTrace)exceptionTrace;
this.name = name;
this.eventSourceName = eventSourceName;
}
[Obsolete("For SMDiagnostics.dll use only. Call DiagnosticUtility.ExceptionUtility instead")]
[MethodImpl(MethodImplOptions.NoInlining)]
#pragma warning disable 56500
internal void TraceFailFast(string message)
{
System.Runtime.Diagnostics.EventLogger logger = null;
try
{
#pragma warning disable 618
logger = new System.Runtime.Diagnostics.EventLogger(this.eventSourceName, this.diagnosticTrace);
#pragma warning restore 618
}
finally
{
#pragma warning disable 618
TraceFailFast(message, logger);
#pragma warning restore 618
}
}
// Fail-- Event Log entry will be generated.
// To force a Watson on a dev machine, do the following:
// 1. Set \HKLM\SOFTWARE\Microsoft\PCHealth\ErrorReporting ForceQueueMode = 0
// 2. In the command environment, set COMPLUS_DbgJitDebugLaunchSetting=0
[Obsolete("For SMDiagnostics.dll use only. Call DiagnosticUtility.ExceptionUtility instead")]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void TraceFailFast(string message, System.Runtime.Diagnostics.EventLogger logger)
{
try
{
if (logger != null)
{
string stackTrace = null;
try
{
stackTrace = new StackTrace().ToString();
}
catch (Exception exception)
{
stackTrace = exception.Message;
}
finally
{
logger.LogEvent(TraceEventType.Critical,
(ushort)EventLogCategory.FailFast,
(uint)EventLogEventId.FailFast,
message,
stackTrace);
}
}
}
catch (Exception e)
{
if (logger != null)
{
logger.LogEvent(TraceEventType.Critical,
(ushort)EventLogCategory.FailFast,
(uint)EventLogEventId.FailFastException,
e.ToString());
}
throw;
}
}
#pragma warning restore 56500
[Obsolete("For SMDiagnostics.dll use only. Call DiagnosticUtility.ExceptionUtility instead")]
internal void TraceFailFastException(Exception exception)
{
TraceFailFast(exception == null ? null : exception.ToString());
}
internal Exception ThrowHelper(Exception exception, TraceEventType eventType, TraceRecord extendedData)
{
#pragma warning disable 618
bool shouldTrace = (this.diagnosticTrace != null && this.diagnosticTrace.ShouldTrace(eventType));
#pragma warning restore 618
if (shouldTrace)
{
using (ExceptionUtility.useStaticActivityId ? Activity.CreateActivity(ExceptionUtility.activityId) : null)
{
this.diagnosticTrace.TraceEvent(eventType, DiagnosticsTraceCode.ThrowingException, LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", "ThrowingException"), TraceSR.GetString(TraceSR.ThrowingException), extendedData, exception, null);
}
IDictionary data = exception.Data;
if (data != null && !data.IsReadOnly && !data.IsFixedSize)
{
object existingString = data[ExceptionStackAsStringKey];
string stackString = existingString == null ? "" : existingString as string;
if (stackString != null)
{
string stack = exception.StackTrace;
if (!string.IsNullOrEmpty(stack))
{
stackString = string.Concat(stackString, stackString.Length == 0 ? "" : Environment.NewLine, "throw", Environment.NewLine, stack, Environment.NewLine, "catch", Environment.NewLine);
data[ExceptionStackAsStringKey] = stackString;
}
}
}
}
// Trace using ETW as well.
exceptionTrace.TraceEtwException(exception, eventType);
return exception;
}
internal Exception ThrowHelper(Exception exception, TraceEventType eventType)
{
return this.ThrowHelper(exception, eventType, null);
}
internal ArgumentException ThrowHelperArgument(string message)
{
return (ArgumentException)this.ThrowHelperError(new ArgumentException(message));
}
internal ArgumentException ThrowHelperArgument(string paramName, string message)
{
return (ArgumentException)this.ThrowHelperError(new ArgumentException(message, paramName));
}
internal ArgumentNullException ThrowHelperArgumentNull(string paramName)
{
return (ArgumentNullException)this.ThrowHelperError(new ArgumentNullException(paramName));
}
internal ArgumentNullException ThrowHelperArgumentNull(string paramName, string message)
{
return (ArgumentNullException)this.ThrowHelperError(new ArgumentNullException(paramName, message));
}
internal ArgumentException ThrowHelperArgumentNullOrEmptyString(string arg)
{
return (ArgumentException)this.ThrowHelperError(new ArgumentException(TraceSR.GetString(TraceSR.StringNullOrEmpty), arg));
}
internal Exception ThrowHelperFatal(string message, Exception innerException)
{
return this.ThrowHelperError(new FatalException(message, innerException));
}
internal Exception ThrowHelperInternal(bool fatal)
{
return fatal ? Fx.AssertAndThrowFatal("Fatal InternalException should never be thrown.") : Fx.AssertAndThrow("InternalException should never be thrown.");
}
internal Exception ThrowHelperInvalidOperation(string message)
{
return ThrowHelperError(new InvalidOperationException(message));
}
internal Exception ThrowHelperCallback(string message, Exception innerException)
{
return this.ThrowHelperCritical(new CallbackException(message, innerException));
}
internal Exception ThrowHelperCallback(Exception innerException)
{
return this.ThrowHelperCallback(TraceSR.GetString(TraceSR.GenericCallbackException), innerException);
}
internal Exception ThrowHelperCritical(Exception exception)
{
return this.ThrowHelper(exception, TraceEventType.Critical);
}
internal Exception ThrowHelperError(Exception exception)
{
return this.ThrowHelper(exception, TraceEventType.Error);
}
internal Exception ThrowHelperWarning(Exception exception)
{
return this.ThrowHelper(exception, TraceEventType.Warning);
}
internal Exception ThrowHelperXml(XmlReader reader, string message)
{
return this.ThrowHelperXml(reader, message, null);
}
internal Exception ThrowHelperXml(XmlReader reader, string message, Exception inner)
{
IXmlLineInfo lineInfo = reader as IXmlLineInfo;
return this.ThrowHelperError(new XmlException(
message,
inner,
(null != lineInfo) ? lineInfo.LineNumber : 0,
(null != lineInfo) ? lineInfo.LinePosition : 0));
}
internal void DiagnosticTraceHandledException(Exception exception, TraceEventType eventType)
{
#pragma warning disable 618
bool shouldTrace = (this.diagnosticTrace != null && this.diagnosticTrace.ShouldTrace(eventType));
#pragma warning restore 618
if (shouldTrace)
{
using (ExceptionUtility.useStaticActivityId ? Activity.CreateActivity(ExceptionUtility.activityId) : null)
{
this.diagnosticTrace.TraceEvent(eventType, DiagnosticsTraceCode.TraceHandledException, LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", "TraceHandledException"), TraceSR.GetString(TraceSR.TraceHandledException), null, exception, null);
}
}
}
// On a single thread, these functions will complete just fine
// and don't need to worry about locking issues because the effected
// variables are ThreadStatic.
internal static void UseActivityId(Guid activityId)
{
ExceptionUtility.activityId = activityId;
ExceptionUtility.useStaticActivityId = true;
}
internal static void ClearActivityId()
{
ExceptionUtility.useStaticActivityId = false;
ExceptionUtility.activityId = Guid.Empty;
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static bool IsInfrastructureException(Exception exception)
{
return exception != null && (exception is ThreadAbortException || exception is AppDomainUnloadedException);
}
}
}
|