public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception, bool getServiceReference) { string eventSource = null; string extendedData = null; string serializedException = null; if (source != null) { eventSource = CreateSourceString(source); } if (traceRecord != null) { StringBuilder sb = new StringBuilder(); XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb, CultureInfo.CurrentCulture)); writer.WriteStartElement(DiagnosticStrings.ExtendedDataTag); traceRecord.WriteTo(writer); writer.WriteEndElement(); extendedData = sb.ToString(); } if (exception != null) { serializedException = DiagnosticTrace.ExceptionToTraceString(exception); } if (getServiceReference && (DiagnosticTrace.traceAnnotation != null)) { return(new TracePayload(serializedException, eventSource, DiagnosticTrace.appDomainFriendlyName, extendedData, DiagnosticTrace.traceAnnotation())); } return(new TracePayload(serializedException, eventSource, DiagnosticTrace.appDomainFriendlyName, extendedData, string.Empty)); }
static string BuildTrace(ref EventDescriptor eventDescriptor, string description, TracePayload payload) { StringBuilder sb = new StringBuilder(); XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb, CultureInfo.CurrentCulture)); writer.WriteStartElement(DiagnosticStrings.TraceRecordTag); writer.WriteAttributeString(DiagnosticStrings.NamespaceTag, DiagnosticTrace.TraceRecordVersion); writer.WriteAttributeString(DiagnosticStrings.SeverityTag, TraceLevelHelper.LookupSeverity((TraceEventLevel)eventDescriptor.Level, (TraceEventOpcode)eventDescriptor.Opcode)); writer.WriteAttributeString(DiagnosticStrings.ChannelTag, DiagnosticTrace.LookupChannel((TraceChannel)eventDescriptor.Channel)); writer.WriteElementString(DiagnosticStrings.TraceCodeTag, DiagnosticTrace.GenerateTraceCode(ref eventDescriptor)); writer.WriteElementString(DiagnosticStrings.DescriptionTag, description); writer.WriteElementString(DiagnosticStrings.AppDomain, payload.AppDomainFriendlyName); if (!string.IsNullOrEmpty(payload.EventSource)) { writer.WriteElementString(DiagnosticStrings.SourceTag, payload.EventSource); } if (!string.IsNullOrEmpty(payload.ExtendedData)) { writer.WriteRaw(payload.ExtendedData); } if (!string.IsNullOrEmpty(payload.SerializedException)) { writer.WriteRaw(payload.SerializedException); } writer.WriteEndElement(); return(sb.ToString()); }
public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTrace diagnosticTrace) { EventLogger logger = new EventLogger(); logger.SetLogSourceName(eventLogSourceName, diagnosticTrace); return(logger); }
public void Event(ref EventDescriptor eventDescriptor, string description) { if (this.TracingEnabled) { TracePayload tracePayload = DiagnosticTrace.GetSerializedPayload(null, null, null); this.WriteTraceSource(ref eventDescriptor, description, tracePayload); } }
public void Event(int eventId, TraceEventLevel traceEventLevel, TraceChannel channel, string description) { if (this.TracingEnabled) { EventDescriptor eventDescriptor = DiagnosticTrace.GetEventDescriptor(eventId, channel, traceEventLevel); this.Event(ref eventDescriptor, description); } }
public EventLogger(string eventLogSourceName, DiagnosticTrace diagnosticTrace) { try { this.diagnosticTrace = diagnosticTrace; //set diagnostics trace prior to calling SafeSetLogSourceName if (canLogEvent) { SafeSetLogSourceName(eventLogSourceName); } } catch (SecurityException) { // running in PT, do not try to log events anymore canLogEvent = false; // not throwing exception on purpose } }
static string ExceptionToTraceString(Exception exception) { StringBuilder sb = new StringBuilder(); XmlTextWriter xml = new XmlTextWriter(new StringWriter(sb, CultureInfo.CurrentCulture)); xml.WriteStartElement(DiagnosticStrings.ExceptionTag); xml.WriteElementString(DiagnosticStrings.ExceptionTypeTag, DiagnosticTrace.XmlEncode(exception.GetType().AssemblyQualifiedName)); xml.WriteElementString(DiagnosticStrings.MessageTag, DiagnosticTrace.XmlEncode(exception.Message)); xml.WriteElementString(DiagnosticStrings.StackTraceTag, DiagnosticTrace.XmlEncode(StackTraceString(exception))); xml.WriteElementString(DiagnosticStrings.ExceptionStringTag, DiagnosticTrace.XmlEncode(exception.ToString())); System.ComponentModel.Win32Exception win32Exception = exception as System.ComponentModel.Win32Exception; if (win32Exception != null) { xml.WriteElementString(DiagnosticStrings.NativeErrorCodeTag, win32Exception.NativeErrorCode.ToString("X", CultureInfo.InvariantCulture)); } if (exception.Data != null && exception.Data.Count > 0) { xml.WriteStartElement(DiagnosticStrings.DataItemsTag); foreach (object dataItem in exception.Data.Keys) { xml.WriteStartElement(DiagnosticStrings.DataTag); xml.WriteElementString(DiagnosticStrings.KeyTag, DiagnosticTrace.XmlEncode(dataItem.ToString())); xml.WriteElementString(DiagnosticStrings.ValueTag, DiagnosticTrace.XmlEncode(exception.Data[dataItem].ToString())); xml.WriteEndElement(); } xml.WriteEndElement(); } if (exception.InnerException != null) { xml.WriteStartElement(DiagnosticStrings.InnerExceptionTag); xml.WriteRaw(ExceptionToTraceString(exception.InnerException)); xml.WriteEndElement(); } xml.WriteEndElement(); return(sb.ToString()); }
public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception) { return(DiagnosticTrace.GetSerializedPayload(source, traceRecord, exception, false)); }
public void TraceTransfer(Guid newId) { Guid oldId = DiagnosticTrace.ActivityId; if (newId != oldId) { try { if (this.HaveListeners) { this.TraceSource.TraceTransfer(0, null, newId); } //also emit to ETW if (this.IsEtwEventEnabled(ref DiagnosticTrace.transferEventDescriptor)) { this.etwProvider.WriteTransferEvent(ref DiagnosticTrace.transferEventDescriptor, newId, DiagnosticTrace.traceAnnotation == null ? string.Empty : DiagnosticTrace.traceAnnotation(), DiagnosticTrace.appDomainFriendlyName); } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } LogTraceFailure(null, e); } } }
void SetLogSourceName(string updatedEventLogSourceName, DiagnosticTrace updatedDiagnosticTrace) { this.eventLogSourceName = updatedEventLogSourceName; this.diagnosticTrace = updatedDiagnosticTrace; }
protected static string XmlEncode(string text) { return(DiagnosticTrace.XmlEncode(text)); }