public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception, bool getServiceReference) { string str = null; string str1 = null; string traceString = null; if (source != null) { str = DiagnosticTrace.CreateSourceString(source); } if (traceRecord != null) { StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)); xmlTextWriter.WriteStartElement("ExtendedData"); traceRecord.WriteTo(xmlTextWriter); xmlTextWriter.WriteEndElement(); str1 = stringBuilder.ToString(); } if (exception != null) { traceString = DiagnosticTrace.ExceptionToTraceString(exception); } return(new TracePayload(traceString, str, DiagnosticTrace.appDomainFriendlyName, str1, string.Empty)); }
private static string ExceptionToTraceString(Exception exception) { StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)); xmlTextWriter.WriteStartElement("Exception"); xmlTextWriter.WriteElementString("ExceptionType", DiagnosticTrace.XmlEncode(exception.GetType().AssemblyQualifiedName)); xmlTextWriter.WriteElementString("Message", DiagnosticTrace.XmlEncode(exception.Message)); xmlTextWriter.WriteElementString("StackTrace", DiagnosticTrace.XmlEncode(DiagnosticTrace.StackTraceString(exception))); xmlTextWriter.WriteElementString("ExceptionString", DiagnosticTrace.XmlEncode(exception.ToString())); Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; xmlTextWriter.WriteElementString("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture)); } if (exception.Data != null && exception.Data.Count > 0) { xmlTextWriter.WriteStartElement("DataItems"); foreach (object key in exception.Data.Keys) { xmlTextWriter.WriteStartElement("Data"); xmlTextWriter.WriteElementString("Key", DiagnosticTrace.XmlEncode(key.ToString())); xmlTextWriter.WriteElementString("Value", DiagnosticTrace.XmlEncode(exception.Data[key].ToString())); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); } if (exception.InnerException != null) { xmlTextWriter.WriteStartElement("InnerException"); xmlTextWriter.WriteRaw(DiagnosticTrace.ExceptionToTraceString(exception.InnerException)); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); return(stringBuilder.ToString()); }