static void LogEvent(TraceEventType type, string code, string description, TraceRecord trace, Exception exception, object source) { StringBuilder traceString = new StringBuilder(SR.GetString(SR.EventLogValue, DiagnosticTrace.ProcessName, DiagnosticTrace.ProcessId.ToString(CultureInfo.CurrentCulture), code, description)); if (source != null) { traceString.AppendLine(SR.GetString(SR.EventLogSourceValue, DiagnosticTrace.CreateSourceString(source))); } if (exception != null) { traceString.AppendLine(SR.GetString(SR.EventLogExceptionValue, exception.ToString())); } if (trace != null) { traceString.AppendLine(SR.GetString(SR.EventLogEventIdValue, trace.EventId)); traceString.AppendLine(SR.GetString(SR.EventLogTraceValue, trace.ToString())); } LogEvent(type, traceString.ToString(), false); }
static XPathNavigator BuildTraceString(PlainXmlWriter xml, TraceEventType type, string code, string description, TraceRecord trace, Exception exception, object source) { xml.WriteStartElement(DiagnosticStrings.TraceRecordTag); xml.WriteAttributeString(DiagnosticStrings.NamespaceTag, DiagnosticTrace.TraceRecordVersion); xml.WriteAttributeString(DiagnosticStrings.SeverityTag, DiagnosticTrace.LookupSeverity(type)); xml.WriteElementString(DiagnosticStrings.TraceCodeTag, code); xml.WriteElementString(DiagnosticStrings.DescriptionTag, description); xml.WriteElementString(DiagnosticStrings.AppDomain, DiagnosticTrace.AppDomainFriendlyName); if (source != null) { xml.WriteElementString(DiagnosticStrings.SourceTag, DiagnosticTrace.CreateSourceString(source)); } if (trace != null) { xml.WriteStartElement(DiagnosticStrings.ExtendedDataTag); xml.WriteAttributeString(DiagnosticStrings.NamespaceTag, trace.EventId); trace.WriteTo(xml); xml.WriteEndElement(); } if (exception != null) { xml.WriteStartElement(DiagnosticStrings.ExceptionTag); DiagnosticTrace.AddExceptionToTraceString(xml, exception); xml.WriteEndElement(); } xml.WriteEndElement(); return(xml.ToNavigator()); }
static internal void TraceEvent(TraceEventType type, string code, string description, TraceRecord trace, Exception exception, ref Guid activityId, bool emitTransfer, object source) { #if DEBUG Debug.Assert(exception == null || type <= TraceEventType.Information); Debug.Assert(!string.IsNullOrEmpty(description), "All TraceCodes should have a description"); #endif if (DiagnosticTrace.ShouldTrace(type)) { using (Activity.CreateActivity(activityId, emitTransfer)) { XPathNavigator navigator = BuildTraceString(type, code, description, trace, exception, source); try { DiagnosticTrace.TraceSource.TraceData(type, 0, navigator); if (DiagnosticTrace.calledShutdown) { DiagnosticTrace.TraceSource.Flush(); } } catch (OutOfMemoryException) { throw; } catch (StackOverflowException) { throw; } catch (ThreadAbortException) { throw; } catch (Exception e) { string traceString = SR.GetString(SR.TraceFailure, type.ToString(), code, description, source == null ? string.Empty : DiagnosticTrace.CreateSourceString(source)); LogTraceFailure(traceString, e); } } } }