protected static void AddExceptionToTraceString(XmlWriter xml, Exception exception) { xml.WriteElementString("ExceptionType", DiagnosticTraceBase.XmlEncode(exception.GetType().AssemblyQualifiedName)); xml.WriteElementString("Message", DiagnosticTraceBase.XmlEncode(exception.Message)); xml.WriteElementString("StackTrace", DiagnosticTraceBase.XmlEncode(DiagnosticTraceBase.StackTraceString(exception))); xml.WriteElementString("ExceptionString", DiagnosticTraceBase.XmlEncode(exception.ToString())); Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; xml.WriteElementString("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture)); } if (exception.Data != null && exception.Data.Count > 0) { xml.WriteStartElement("DataItems"); foreach (object key in exception.Data.Keys) { xml.WriteStartElement("Data"); xml.WriteElementString("Key", DiagnosticTraceBase.XmlEncode(key.ToString())); xml.WriteElementString("Value", DiagnosticTraceBase.XmlEncode(exception.Data[key].ToString())); xml.WriteEndElement(); } xml.WriteEndElement(); } if (exception.InnerException != null) { xml.WriteStartElement("InnerException"); DiagnosticTraceBase.AddExceptionToTraceString(xml, exception.InnerException); xml.WriteEndElement(); } }
private static void WriteExceptionToTraceString(XmlTextWriter xml, Exception exception, int remainingLength, int remainingAllowedRecursionDepth) { if (remainingAllowedRecursionDepth >= 1) { if (EtwDiagnosticTrace.WriteStartElement(xml, "Exception", ref remainingLength)) { try { List <Tuple <string, string> > tuples = new List <Tuple <string, string> >(); tuples.Add(new Tuple <string, string>("ExceptionType", DiagnosticTraceBase.XmlEncode(exception.GetType().AssemblyQualifiedName))); tuples.Add(new Tuple <string, string>("Message", DiagnosticTraceBase.XmlEncode(exception.Message))); tuples.Add(new Tuple <string, string>("StackTrace", DiagnosticTraceBase.XmlEncode(DiagnosticTraceBase.StackTraceString(exception)))); tuples.Add(new Tuple <string, string>("ExceptionString", DiagnosticTraceBase.XmlEncode(exception.ToString()))); IList <Tuple <string, string> > tuples1 = tuples; Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; tuples1.Add(new Tuple <string, string>("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture))); } foreach (Tuple <string, string> tuple in tuples1) { if (EtwDiagnosticTrace.WriteXmlElementString(xml, tuple.Item1, tuple.Item2, ref remainingLength)) { continue; } return; } if (exception.Data != null && exception.Data.Count > 0) { string exceptionData = EtwDiagnosticTrace.GetExceptionData(exception); if (exceptionData.Length < remainingLength) { xml.WriteRaw(exceptionData); remainingLength = remainingLength - exceptionData.Length; } } if (exception.InnerException != null) { string innerException = EtwDiagnosticTrace.GetInnerException(exception, remainingLength, remainingAllowedRecursionDepth - 1); if (!string.IsNullOrEmpty(innerException) && innerException.Length < remainingLength) { xml.WriteRaw(innerException); } } } finally { xml.WriteEndElement(); } return; } else { return; } } else { return; } }