Esempio n. 1
0
        private void TraceFilterTestHelper(
            Action <ApplicationInsightsTraceListener, TraceEventCache> callTraceEent,
            bool shouldTrace,
            SourceLevels filterLevel = SourceLevels.Warning)
        {
            TraceEventCache shimTraceEventCache = new TraceEventCache();

            using (var traceListener = new ApplicationInsightsTraceListener(this.adapterHelper.InstrumentationKey))
            {
                var telemetryConfiguration = new TelemetryConfiguration
                {
                    InstrumentationKey = Guid.NewGuid().ToString(),
                    TelemetryChannel   = this.adapterHelper.Channel
                };

                traceListener.TelemetryClient = new TelemetryClient(telemetryConfiguration);

                var traceFilter = new EventTypeFilter(filterLevel);
                traceListener.Filter = traceFilter;

                callTraceEent(traceListener, shimTraceEventCache);

                Assert.AreEqual(shouldTrace, this.adapterHelper.Channel.SentItems.Length == 1);
            }
        }
        private void ValidateASingleMessageActionBased(
            Action <ApplicationInsightsTraceListener, TraceEventCache> callTraceAction,
            string instrumentationKey,
            TraceOptions options)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

            using (var listener = new ApplicationInsightsTraceListener(instrumentationKey))
            {
                listener.TraceOutputOptions = options;
                TraceEventCache traceEventCache = new TraceEventCache();
#if NET452
                PrivateObject privateObject = new PrivateObject(traceEventCache);
                privateObject.SetField("timeStamp", DateTime.Now.Ticks);
                privateObject.SetField("stackTrace", "Environment.StackTrace");
#else
                TypeInfo traceEventCacheType = traceEventCache.GetType().GetTypeInfo();
                traceEventCacheType.GetDeclaredField("_timeStamp").SetValue(traceEventCache, DateTime.Now.Ticks);
#endif
                callTraceAction(listener, traceEventCache);

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
                Assert.IsNotNull(telemetry, "didn't got the event trace to the inner channel");
                Assert.AreEqual(telemetry.Context.InstrumentationKey, instrumentationKey);
            }
        }
Esempio n. 3
0
        public void FlushesOnDispose()
        {
            TelemetryChannel.ResetTelemetry();
            var listener = new ApplicationInsightsTraceListener(string.Empty);

            listener.Dispose();

            Assert.AreEqual(1, TelemetryChannel.FlushCount);
        }
Esempio n. 4
0
        public void InstrumentationKeyIsSetProperly()
        {
            var listener = new ApplicationInsightsTraceListener("Key1");

            Assert.AreEqual("Key1", listener.InstrumentationKey);

            listener.InstrumentationKey = "Key2";
            Assert.AreEqual("Key2", listener.InstrumentationKey);
        }
Esempio n. 5
0
        /// <summary>
        /// Static constructor for initializing Application Insights trace listener
        /// </summary>
        static Tracer()
        {
            var instrumentationKey = ConfigurationManager.AppSettings["ApplicationInsightsInstrumentationKey"];

            if (!string.IsNullOrWhiteSpace(instrumentationKey))
            {
                ApplicationInsightsListener = new ApplicationInsightsTraceListener(instrumentationKey);
            }
        }
        public void TraceListenerWriteUsedApplicationInsightsConfigInstrumentationKeyWhenUnspecifiedInstrumentationKey()
        {
            // Changing the channel to Mock channel to verify 
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(null))
            {                
                this.VerifyMessagesInMockChannel(listener, this.adapterHelper.InstrumentationKey);
            }
        }
        private void VerifyMessagesInMockChannel(
            ApplicationInsightsTraceListener aiListener,
            string instrumentationKey)
        {
            aiListener.Write("Sample Write message");
            aiListener.WriteLine("Sample WriteLine message");
            aiListener.TraceEvent(new TraceEventCache(), "Sample TraceEvent", TraceEventType.Error, 0);
            aiListener.TraceData(new TraceEventCache(), "Sample warning message GUID:{0}", TraceEventType.Information, 0, Guid.NewGuid());

            AdapterHelper.ValidateChannel(this.adapterHelper, instrumentationKey, 4);
        }
        public void TraceListenerWriteUsedApplicationInsightsConfigInstrumentationKeyWhenUnspecifiedInstrumentationKey()
        {
            // Changing the channel to Mock channel to verify
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(null))
            {
                this.VerifyMessagesInMockChannel(listener, this.adapterHelper.InstrumentationKey);
            }
        }
        public void TraceListenerWriteNotUsingAppliocationInsightsConfigInstrumentationKeyWhenspecifiedInstrumentationKey()
        {
            // Changing the channel to Mock channel to verify
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
            string instrumentationKey = Guid.NewGuid().ToString();

            using (var listener = new ApplicationInsightsTraceListener(instrumentationKey))
            {
                this.VerifyMessagesInMockChannel(listener, instrumentationKey);
            }
        }
        public void TraceListenerWriteNotUsingAppliocationInsightsConfigInstrumentationKeyWhenspecifiedInstrumentationKey()
        {
            // Changing the channel to Mock channel to verify 
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
            string instrumentationKey = Guid.NewGuid().ToString();

            using (var listener = new ApplicationInsightsTraceListener(instrumentationKey))
            {
                this.VerifyMessagesInMockChannel(listener, instrumentationKey);
            }
        }
        public void TraceEventDoesNotStoreCallStackInTelemetryPropertiesBecauseLongValuesAreRejected()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
            var listener = new ApplicationInsightsTraceListener();

            listener.TraceOutputOptions = TraceOptions.Callstack;

            listener.TraceEvent(new TraceEventCache(), string.Empty, TraceEventType.Information, default(int));

            var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.Single();

            Assert.IsFalse(telemetry.Properties.ContainsKey("CallStack"));
        }
        public void TelemetryIsAcceptedByValidateEndpoint()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                Assert.IsNull(TelemetrySender.ValidateEndpointSend(telemetry));
            }
        }
        public void TelemetryIsAcceptedByValidateEndpoint()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                Assert.IsNull(TelemetrySender.ValidateEndpointSend(telemetry));
            }
        }
        public void SdkVersionIsCorrect()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                string expectedVersion = SdkVersionHelper.GetExpectedSdkVersion(typeof(ApplicationInsightsTraceListener), prefix: "sd:");
                Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion);
            }
        }
        public void SdkVersionIsCorrect()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                string expectedVersion = SdkVersionHelper.GetExpectedSdkVersion(prefix: "sd:");
                Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion);
            }
        }
        public void TelemetryIsAcceptedByValidateEndpoint()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                Assert.IsNull(TelemetrySender.ValidateEndpointSend(telemetry));
            }
        }
        public void TraceListenerWriteUsedApplicationInsightsConfigInstrumentationKeyWhenUnspecifiedInstrumentationKey()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            TelemetryConfiguration.Active.InstrumentationKey = this.adapterHelper.InstrumentationKey;

            // Changing the channel to Mock channel to verify
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

            using (var listener = new ApplicationInsightsTraceListener(null))
            {
                this.VerifyMessagesInMockChannel(listener, this.adapterHelper.InstrumentationKey);
            }
        }
        public void SdkVersionIsCorrect()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                listener.WriteLine("A Message to write line");

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();

                string expectedVersion = SdkVersionHelper.GetExpectedSdkVersion(prefix: "sd:", loggerType: typeof(ApplicationInsightsTraceListener));
                Assert.AreEqual(expectedVersion, telemetry.Context.GetInternalContext().SdkVersion);
            }
        }
        public void TraceListenerFlushesChannel()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                try
                {
                    listener.Flush();
                    Assert.Fail();
                }
                catch (Exception ex)
                {
                    Assert.AreEqual("Flush called", ex.Message);
                }
            }
        }
Esempio n. 20
0
        public void ActivityEventTypesAreTreatedAsVerbose(TraceEventType eventType)
        {
            TelemetryChannel.ResetTelemetry();
            using (var listener = new ApplicationInsightsTraceListener(string.Empty))
            {
                var source = new LogSource("TestSource", new[] { listener }, SourceLevels.All);

                source.TraceData(TraceEventType.Information, 1, new LogEntry(string.Empty, "TestCategory", 1, 1, eventType, string.Empty, null));
            }

            Assert.AreEqual(TelemetryChannel.Traces.Count, 1);
            var trace = TelemetryChannel.Traces.Single() as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual(SeverityLevel.Verbose, trace.SeverityLevel);
            Assert.AreEqual(eventType.ToString(), trace.Properties["LoggedSeverity"]);
        }
        public void TraceListenerWriteLineUseApplicationEventSourceToLogMessage()
        {
            // Changing the channel to Mock channel to verify 
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                var expectedMessage = "A Message to write line";
                listener.WriteLine(expectedMessage);

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();
                Assert.AreEqual(expectedMessage, telemetry.Message);
                Assert.IsFalse(telemetry.Properties.ContainsKey("EventId"));
                Assert.AreEqual(SeverityLevel.Verbose, telemetry.SeverityLevel);
            }
        }
        public void TraceListenerWriteLineUseApplicationEventSourceToLogMessage()
        {
            // Changing the channel to Mock channel to verify
            // the Telemetry event is assigned with the InstrumentationKey from configuration
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                var expectedMessage = "A Message to write line";
                listener.WriteLine(expectedMessage);

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First();
                Assert.AreEqual(expectedMessage, telemetry.Message);
                Assert.IsFalse(telemetry.Properties.ContainsKey("EventId"));
                Assert.AreEqual(SeverityLevel.Verbose, telemetry.SeverityLevel);
            }
        }
Esempio n. 23
0
        public void WriteLineCallsWrite()
        {
            TelemetryChannel.ResetTelemetry();
            using (var listener = new ApplicationInsightsTraceListener(string.Empty))
            {
                var source = new LogSource("TestSource", new[] { listener }, SourceLevels.All);

                listener.WriteLine("Test message");
            }

            Assert.AreEqual(TelemetryChannel.Traces.Count, 1);
            var trace = TelemetryChannel.Traces.Single() as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual("Test message", trace.Message);
            Assert.AreEqual(0, trace.Properties.Count);
            Assert.AreEqual(SeverityLevel.Information, trace.SeverityLevel);
        }
Esempio n. 24
0
        public void StringMessagesAreWrittenAsEvents()
        {
            TelemetryChannel.ResetTelemetry();
            using (var listener = new ApplicationInsightsTraceListener(string.Empty))
            {
                var source = new LogSource("TestSource", new[] { listener }, SourceLevels.All);

                listener.TraceData(new TraceEventCache(), "Test source", TraceEventType.Error, 10, "Test message");
            }

            Assert.AreEqual(TelemetryChannel.Traces.Count, 1);
            var trace = TelemetryChannel.Traces.Single() as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual("Test message", trace.Message);
            Assert.AreEqual(0, trace.Properties.Count);
            Assert.AreEqual(SeverityLevel.Error, trace.SeverityLevel);
        }
Esempio n. 25
0
        public void NonEventNonStringObjectsLoggedAsStrings()
        {
            TelemetryChannel.ResetTelemetry();
            using (var listener = new ApplicationInsightsTraceListener(string.Empty))
            {
                var source = new LogSource("TestSource", new[] { listener }, SourceLevels.All);

                listener.TraceData(new TraceEventCache(), "Test source", TraceEventType.Warning, 10, 1);
            }

            Assert.AreEqual(TelemetryChannel.Traces.Count, 1);
            var trace = TelemetryChannel.Traces.Single() as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual("1", trace.Message);
            Assert.AreEqual(0, trace.Properties.Count);
            Assert.AreEqual(SeverityLevel.Warning, trace.SeverityLevel);
        }
        public void TraceListenerFlushesChannel()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
#pragma warning restore CS0618 // Type or member is obsolete

            using (var listener = new ApplicationInsightsTraceListener(Guid.NewGuid().ToString()))
            {
                try
                {
                    listener.Flush();
                    Assert.Fail();
                }
                catch (Exception ex)
                {
                    Assert.AreEqual("Flush called", ex.Message);
                }
            }
        }
        private void ValidateASingleMessageActionBased(
            Action <ApplicationInsightsTraceListener, TraceEventCache> callTraceAction,
            string instrumentationKey,
            TraceOptions options)
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(instrumentationKey))
            {
                listener.TraceOutputOptions = options;
                TraceEventCache traceEventCache = new TraceEventCache();
                PrivateObject   privateObject   = new PrivateObject(traceEventCache);
                privateObject.SetField("timeStamp", DateTime.Now.Ticks);
                privateObject.SetField("stackTrace", "Environment.StackTrace");

                callTraceAction(listener, traceEventCache);

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
                Assert.IsNotNull(telemetry, "didn't got the event trace to the inner channel");
                Assert.AreEqual(telemetry.Context.InstrumentationKey, instrumentationKey);
            }
        }
Esempio n. 28
0
        public void TelemetryIsSentToApplicationInsights()
        {
            var key        = "TestKey";
            var message    = "Test message";
            var category   = "Test category";
            var priority   = 5;
            var eventId    = 11;
            var eventType  = TraceEventType.Warning;
            var title      = "Test title";
            var testValue  = "Test value";
            var properties = new Dictionary <string, object>()
            {
                { "Test property", testValue }
            };

            TelemetryChannel.ResetTelemetry();
            using (var listener = new ApplicationInsightsTraceListener(key))
            {
                var source = new LogSource("TestSource", new[] { listener }, SourceLevels.All);

                source.TraceData(TraceEventType.Information, 1, new LogEntry(message, category, priority, eventId, eventType, title, properties));
            }

            Assert.AreEqual(TelemetryChannel.Traces.Count, 1);
            var trace = TelemetryChannel.Traces.Single() as TraceTelemetry;

            Assert.IsNotNull(trace);
            Assert.AreEqual(message, trace.Message);
            Assert.AreEqual(SeverityLevel.Warning, trace.SeverityLevel);
            Assert.AreEqual(title, trace.Properties["Title"]);
            Assert.AreEqual(category, trace.Properties["Categories"]);
            Assert.AreEqual(priority.ToString(), trace.Properties["Priority"]);
            Assert.AreEqual(Environment.MachineName, trace.Properties["MachineName"]);
            Assert.AreEqual(eventId.ToString(), trace.Properties["EventId"]);
            Assert.AreEqual(testValue, trace.Properties["Test property"]);
            Assert.AreEqual(key, trace.Context.InstrumentationKey);
        }
        private void TraceFilterTestHelper(
            Action<ApplicationInsightsTraceListener, TraceEventCache> callTraceEent,
            bool shouldTrace,
            SourceLevels filterLevel = SourceLevels.Warning)
        {
            TraceEventCache shimTraceEventCache = new TraceEventCache();

            using (var traceListener = new ApplicationInsightsTraceListener(this.adapterHelper.InstrumentationKey))
            {
                var telemetryConfiguration = new TelemetryConfiguration
                {
                    InstrumentationKey = Guid.NewGuid().ToString(),
                    TelemetryChannel = this.adapterHelper.Channel
                };

                traceListener.TelemetryClient = new TelemetryClient(telemetryConfiguration);

                var traceFilter = new EventTypeFilter(filterLevel);
                traceListener.Filter = traceFilter;

                callTraceEent(traceListener, shimTraceEventCache);

                Assert.AreEqual(shouldTrace, this.adapterHelper.Channel.SentItems.Length == 1);
            }
        }
 public void TraceListenerInitializeDoesNotThrowWhenInstrumentationKeyIsNull()
 {
     var listener = new ApplicationInsightsTraceListener(null);
     listener.Dispose();
 }
        public void TraceEventDoesNotStoreCallStackInTelemetryPropertiesBecauseLongValuesAreRejected()
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;
            var listener = new ApplicationInsightsTraceListener();
            listener.TraceOutputOptions = TraceOptions.Callstack;

            listener.TraceEvent(new TraceEventCache(), string.Empty, TraceEventType.Information, default(int));

            var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.Single();
            Assert.IsFalse(telemetry.Properties.ContainsKey("CallStack"));
        }
        public void TraceListenerInitializeTDoesNoThrowWhenInstrumentationKeyIsEmpty()
        {
            var listener = new ApplicationInsightsTraceListener(string.Empty);

            listener.Dispose();
        }
        private void ValidateASingleMessageActionBased(
            Action<ApplicationInsightsTraceListener, TraceEventCache> callTraceAction,
            string instrumentationKey,
            TraceOptions options)
        {
            TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel;

            using (var listener = new ApplicationInsightsTraceListener(instrumentationKey))
            {
                listener.TraceOutputOptions = options;
                TraceEventCache traceEventCache = new TraceEventCache();
                PrivateObject privateObject = new PrivateObject(traceEventCache);
                privateObject.SetField("timeStamp", DateTime.Now.Ticks);
                privateObject.SetField("stackTrace", "Environment.StackTrace");

                callTraceAction(listener, traceEventCache);

                TraceTelemetry telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault();
                Assert.IsNotNull(telemetry, "didn't got the event trace to the inner channel");
                Assert.AreEqual(telemetry.Context.InstrumentationKey, instrumentationKey);
            }
        }
        public void TraceListenerInitializeDoesNotThrowWhenInstrumentationKeyIsNull()
        {
            var listener = new ApplicationInsightsTraceListener(null);

            listener.Dispose();
        }
        private void VerifyMessagesInMockChannel(
            ApplicationInsightsTraceListener aiListener,
            string instrumentationKey)
        {
            aiListener.Write("Sample Write message");
            aiListener.WriteLine("Sample WriteLine message");
            aiListener.TraceEvent(new TraceEventCache(), "Sample TraceEvent", TraceEventType.Error, 0);
            aiListener.TraceData(new TraceEventCache(), "Sample warning message GUID:{0}", TraceEventType.Information, 0, Guid.NewGuid());

            AdapterHelper.ValidateChannel(this.adapterHelper, instrumentationKey, 4);
        }
 public void TraceListenerInitializeTDoesNoThrowWhenInstrumentationKeyIsEmpty()
 {
     var listener = new ApplicationInsightsTraceListener(string.Empty);
     listener.Dispose();
 }