Пример #1
0
        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));
        }
Пример #2
0
        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());
        }
Пример #3
0
        public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTrace diagnosticTrace)
        {
            EventLogger logger = new EventLogger();

            logger.SetLogSourceName(eventLogSourceName, diagnosticTrace);
            return(logger);
        }
Пример #4
0
 public void Event(ref EventDescriptor eventDescriptor, string description)
 {
     if (this.TracingEnabled)
     {
         TracePayload tracePayload = DiagnosticTrace.GetSerializedPayload(null, null, null);
         this.WriteTraceSource(ref eventDescriptor, description, tracePayload);
     }
 }
Пример #5
0
 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);
     }
 }
Пример #6
0
 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
     }
 }
Пример #7
0
        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());
        }
Пример #8
0
 public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception)
 {
     return(DiagnosticTrace.GetSerializedPayload(source, traceRecord, exception, false));
 }
Пример #9
0
        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);
                }
            }
        }
Пример #10
0
 void SetLogSourceName(string updatedEventLogSourceName, DiagnosticTrace updatedDiagnosticTrace)
 {
     this.eventLogSourceName = updatedEventLogSourceName;
     this.diagnosticTrace    = updatedDiagnosticTrace;
 }
Пример #11
0
 protected static string XmlEncode(string text)
 {
     return(DiagnosticTrace.XmlEncode(text));
 }