public void Send(TraceEvent eventData)
 {
     try
     {
         if (eventData.MetaData != null && !string.IsNullOrEmpty(eventData.MetaData.MessageFormat))
         {
             // Check if trace message is sended to the portal (somewhere before in the stack)
             // It allows to avoid infinite recursion if sending to the portal traces something.
             if (!ThreadResourceLock.IsResourceLocked)
             {
                 using (var portalSenderLock = new ThreadResourceLock())
                 {
                     try
                     {
                         if (!this.throttlingManager.ThrottleEvent(eventData.MetaData.EventId, eventData.MetaData.Keywords))
                         {
                             this.InternalSendTraceTelemetry(eventData);
                         }
                     }
                     catch (Exception exp)
                     {
                         // This message will not be sent to the portal because we have infinite loop protection
                         // But it will be available in PerfView or StatusMonitor
                         CoreEventSource.Log.LogError("Failed to send traces to the portal: " + exp.ToInvariantString());
                     }
                 }
             }
         }
     }
     catch (Exception)
     {
         // We were trying to send traces out and failed. 
         // No reason to try to trace something else again
     }
 }
 public void Send(TraceEvent eventData)
 {
     if (!this.IsDisabled)
     {
         this.EventData.Add(eventData);
     }
 }
 public void WriteEvent(TraceEvent eventData)
 {
     if (eventData.MetaData != null && eventData.MetaData.MessageFormat != null)
     {
         // check severity because it is not done in Silverlight EventSource implementation 
         if (eventData.MetaData.Level <= this.LogLevel)
         {
             foreach (var sender in this.diagnosticsSenders)
             {
                 sender.Send(eventData);
             }
         }
     }
 }
        public void Send(TraceEvent eventData)
        {
            if (this.debugOutput.IsLogging())
            {
                if (eventData.MetaData != null && !string.IsNullOrEmpty(eventData.MetaData.MessageFormat))
                {
                    var message = eventData.Payload != null && eventData.Payload.Length > 0 ?
                        string.Format(CultureInfo.InvariantCulture, eventData.MetaData.MessageFormat, eventData.Payload) :
                        eventData.MetaData.MessageFormat;

                    this.debugOutput.WriteLine(message);
                }
            }
        }
        protected override void OnEventWritten(EventWrittenEventArgs eventSourceEvent)
        {
            var metadata = new EventMetaData
            {
                Keywords = (long)eventSourceEvent.Keywords,
                MessageFormat = eventSourceEvent.Message,
                EventId = eventSourceEvent.EventId,
                Level = eventSourceEvent.Level
            };

            var traceEvent = new TraceEvent
            {
                MetaData = metadata,
                Payload = eventSourceEvent.Payload != null ? eventSourceEvent.Payload.ToArray() : null
            };

            this.listener.WriteEvent(traceEvent);
        }
        public void TestLogMessageWithEmptyPayload()
        {
            var senderMock = new F5DiagnosticsSenderMock();
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Error occured"
                },
                Payload = null
            };

            senderMock.Send(evt);
            Assert.AreEqual(1, senderMock.Messages.Count);
            Assert.AreEqual("Error occured", senderMock.Messages[0]);
        }
        public void TestLogMessage()
        {
            var senderMock = new F5DiagnosticsSenderMock();
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Error occured at {0}, {1}"
                },
                Payload = new[] { "My function", "some failure" }
            };

            senderMock.Send(evt);
            Assert.AreEqual(1, senderMock.Messages.Count);
            Assert.AreEqual("Error occured at My function, some failure", senderMock.Messages[0]);
        }
        public void SendNotFailIfChannelNotInitialized()
        {
            var configuration = new TelemetryConfiguration();
            var portalSenderWithDefaultCOnfiguration = new PortalDiagnosticsSender(
                configuration,
                this.dontThrottleManager);

            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId       = 10,
                    Keywords      = 0x20,
                    Level         = EventLevel.Warning,
                    MessageFormat = "Something failed"
                },
                Payload = null
            };

            portalSenderWithDefaultCOnfiguration.Send(evt);
        }
        public void SendNotFailIfChannelNotInitialized()
        {
            var configuration = new TelemetryConfiguration();
            var portalSenderWithDefaultCOnfiguration = new PortalDiagnosticsSender(
                configuration,
                this.dontThrottleManager);

            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Something failed"
                },
                Payload = null
            };

            portalSenderWithDefaultCOnfiguration.Send(evt);
        }
        protected void WriteEvent(int eventId, params object[] parameters)
        {
            if (this.eventListener != null)
            {
                if (this.metadataCollection != null && this.metadataCollection.ContainsKey(eventId))
                {
                    var traceEvent = new TraceEvent
                    {
                        MetaData = this.metadataCollection[eventId],
                        Payload = parameters
                    };

                    this.eventListener.WriteEvent(traceEvent);
                }
#if DEBUG
                else
                {
                    throw new InvalidDataException("EventId is not in metadata collection: " + eventId);
                }
#endif
            }
        }
示例#11
0
        protected void WriteEvent(int eventId, params object[] parameters)
        {
            if (this.eventListener != null)
            {
                if (this.metadataCollection != null && this.metadataCollection.ContainsKey(eventId))
                {
                    var traceEvent = new TraceEvent
                    {
                        MetaData = this.metadataCollection[eventId],
                        Payload  = parameters
                    };

                    this.eventListener.WriteEvent(traceEvent);
                }
#if DEBUG
                else
                {
                    throw new InvalidDataException("EventId is not in metadata collection: " + eventId);
                }
#endif
            }
        }
示例#12
0
        protected override void OnEventWritten(EventWrittenEventArgs eventSourceEvent)
        {
            if (eventSourceEvent == null)
            {
                return;
            }

            var metadata = new EventMetaData
            {
                Keywords      = (long)eventSourceEvent.Keywords,
                MessageFormat = eventSourceEvent.Message,
                EventId       = eventSourceEvent.EventId,
                Level         = eventSourceEvent.Level
            };

            var traceEvent = new TraceEvent
            {
                MetaData = metadata,
                Payload  = eventSourceEvent.Payload != null?eventSourceEvent.Payload.ToArray() : null
            };

            this.listener.WriteEvent(traceEvent);
        }
        public void TestSendingOfEvent()
        {
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Error occured at {0}, {1}"
                },
                Payload = new[] { "My function", "some failure" }
            };

            this.nonThrottlingPortalSender.Send(evt);

            Assert.AreEqual(1, this.sendItems.Count);
            var trace = this.sendItems[0] as TraceTelemetry;
            Assert.IsNotNull(trace);
            Assert.AreEqual(
                "AI (Internal): Error occured at My function, some failure", 
                trace.Message);
            Assert.AreEqual(2, trace.Properties.Count);
        }
示例#14
0
        public void TestSendingOfEvent()
        {
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId       = 10,
                    Keywords      = 0x20,
                    Level         = EventLevel.Warning,
                    MessageFormat = "Error occured at {0}, {1}"
                },
                Payload = new[] { "My function", "some failure" }
            };

            this.nonThrottlingPortalSender.Send(evt);

            Assert.AreEqual(1, this.sendItems.Count);
            var trace = this.sendItems[0] as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual(
                "AI (Internal): Error occured at My function, some failure",
                trace.Message);
        }
        private void InternalSendTraceTelemetry(TraceEvent eventData)
        {
            if (this.telemetryClient.TelemetryConfiguration.TelemetryChannel == null)
            {
                return;
            }

            var traceTelemetry = new TraceTelemetry();

            string message = eventData.Payload != null ?
                string.Format(CultureInfo.CurrentCulture, eventData.MetaData.MessageFormat, eventData.Payload.ToArray()) :
                eventData.MetaData.MessageFormat;

            // Add "AI: " prefix (if keyword does not contain UserActionable = (EventKeywords)0x1, than prefix should be "AI (Internal):" )
            if ((eventData.MetaData.Keywords & EventSourceKeywords.UserActionable) == EventSourceKeywords.UserActionable)
            {
                message = AiPrefix + message;
            }
            else
            {
                message = AiNonUserActionable + message;
            }

            traceTelemetry.Message = message;
            if (!string.IsNullOrEmpty(this.DiagnosticsInstrumentationKey))
            {
                traceTelemetry.Context.InstrumentationKey = this.DiagnosticsInstrumentationKey;
            }

            traceTelemetry.Context.Operation.SyntheticSource = SdkTelemetrySyntheticSourceName;

            this.telemetryClient.TrackTrace(traceTelemetry);
        }
        private void InternalSendTraceTelemetry(TraceEvent eventData)
        {
            if (this.telemetryClient.TelemetryConfiguration.TelemetryChannel == null)
            {
                return;
            }

            var traceTelemetry = new TraceTelemetry();
            
            string message;
            if (eventData.Payload != null)
            {
                const string ParameterNameFormat = "arg{0}";
                    
                for (int i = 1; i <= eventData.Payload.Count(); i++)
                {
                    traceTelemetry.Properties.Add(
                        string.Format(CultureInfo.CurrentCulture, ParameterNameFormat, i),
                        eventData.Payload[i - 1].ToString());
                }

                message = string.Format(CultureInfo.CurrentCulture, eventData.MetaData.MessageFormat, eventData.Payload.ToArray());
            }
            else
            {
                message = eventData.MetaData.MessageFormat;
            }

            // Add "AI: " prefix (if keyword does not contain UserActionable = (EventKeywords)0x1, than prefix should be "AI (Internal):" )
            if ((eventData.MetaData.Keywords & EventSourceKeywords.UserActionable) == EventSourceKeywords.UserActionable)
            {
                message = AiPrefix + message;
            }
            else
            {
                message = AiNonUserActionable + message;
            }

            traceTelemetry.Message = message;
            if (!string.IsNullOrEmpty(this.DiagnosticsInstrumentationKey))
            {
                traceTelemetry.Context.InstrumentationKey = this.DiagnosticsInstrumentationKey;
            }

            this.telemetryClient.TrackTrace(traceTelemetry);
        }
 public void Send(TraceEvent eventData)
 {
     this.EventData.Add(eventData);
 }
 public void Send(TraceEvent eventData)
 {
     this.EventData.Add(eventData);
 }
        public void TestSendingWithSeparateInstrumentationKey()
        {
            var diagnosticsInstrumentationKey = Guid.NewGuid().ToString();
            this.nonThrottlingPortalSender.DiagnosticsInstrumentationKey = diagnosticsInstrumentationKey;
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Error occured at {0}, {1}"
                },
                Payload = new[] { "My function", "some failure" }
            };

            this.nonThrottlingPortalSender.Send(evt);

            Assert.AreEqual(1, this.sendItems.Count);
            var trace = this.sendItems[0] as TraceTelemetry;
            Assert.IsNotNull(trace);
            Assert.AreEqual(diagnosticsInstrumentationKey, trace.Context.InstrumentationKey);
        }
        public void TestSendingEmptyPayload()
        {
            var evt = new TraceEvent
            {
                MetaData = new EventMetaData
                {
                    EventId = 10,
                    Keywords = 0x20,
                    Level = EventLevel.Warning,
                    MessageFormat = "Something failed"
                },
                Payload = null
            };

            this.nonThrottlingPortalSender.Send(evt);

            Assert.AreEqual(1, this.sendItems.Count);
            var trace = this.sendItems[0] as TraceTelemetry;
            Assert.IsNotNull(trace);
            Assert.AreEqual(
                "AI (Internal): Something failed",
                trace.Message);
            Assert.AreEqual(0, trace.Properties.Count);
        }