Esempio n. 1
0
        public void ApplicationInsightsLoggerDoesNotAddScopeWhenSwitchIsFalse()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            // Case where Scope is NOT Included
            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => itemsReceived.Add(telemetryItem),
                configureTelemetryConfiguration: null,
                configureApplicationInsightsOptions: (appInsightsLoggerOptions) => appInsightsLoggerOptions.IncludeScopes = false);

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            using (testLogger.BeginScope("TestScope"))
            {
                using (testLogger.BeginScope <IReadOnlyCollection <KeyValuePair <string, object> > >(new Dictionary <string, object> {
                    { "Key", "Value" }
                }))
                {
                    testLogger.LogInformation("Testing");
                    testLogger.LogError(new Exception("ExceptionMessage"), "LoggerMessage");
                }
            }

            Assert.IsFalse((itemsReceived[0] as ISupportProperties).Properties.ContainsKey("Scope"));
            Assert.IsFalse((itemsReceived[0] as ISupportProperties).Properties.ContainsKey("Key"));

            Assert.IsFalse((itemsReceived[1] as ISupportProperties).Properties.ContainsKey("Scope"));
            Assert.IsFalse((itemsReceived[1] as ISupportProperties).Properties.ContainsKey("Key"));

            Assert.AreEqual("Testing", (itemsReceived[0] as TraceTelemetry).Message);
            Assert.AreEqual("ExceptionMessage", (itemsReceived[1] as ExceptionTelemetry).Message);
        }
        public void ApplicationInsightsLoggerLogsExceptionAsTraceWhenSwitchIsFalse()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => itemsReceived.Add(telemetryItem),
                configureTelemetryConfiguration: null,
                configureApplicationInsightsOptions: (appInsightsLoggerOptions) => appInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry = false);

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            testLogger.LogInformation("Testing");
            testLogger.LogError(new Exception("ExceptionMessage"), "LoggerMessage");

            Assert.IsInstanceOfType(itemsReceived[0], typeof(TraceTelemetry));
            Assert.IsInstanceOfType(itemsReceived[1], typeof(TraceTelemetry));

            Assert.AreEqual(SeverityLevel.Information, (itemsReceived[0] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual("Testing", (itemsReceived[0] as TraceTelemetry).Message);

            Assert.AreEqual(SeverityLevel.Error, (itemsReceived[1] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual("LoggerMessage", (itemsReceived[1] as TraceTelemetry).Message);

            Assert.AreEqual("ExceptionMessage", (itemsReceived[1] as TraceTelemetry).Properties["ExceptionMessage"]);
        }
Esempio n. 3
0
        public void DefaultLoggerOptionsAreCorrectlyRegistered()
        {
            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => { });

            IOptions <ApplicationInsightsLoggerOptions> registeredOptions =
                serviceProvider.GetRequiredService <IOptions <ApplicationInsightsLoggerOptions> >();

            Assert.IsTrue(registeredOptions.Value.TrackExceptionsAsExceptionTelemetry);
            Assert.IsTrue(registeredOptions.Value.IncludeScopes);
        }
Esempio n. 4
0
        public void TelemetryChannelIsFlushedWhenServiceProviderIsDisposed()
        {
            TestTelemetryChannel testTelemetryChannel = new TestTelemetryChannel();

            using (ServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                       delegate { },
                       telemetryConfiguration => telemetryConfiguration.TelemetryChannel = testTelemetryChannel))
            {
                serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();
            }

            Assert.AreEqual(1, testTelemetryChannel.FlushCount);
        }
Esempio n. 5
0
        public void TelemetryChannelIsNotFlushedWhenFlushOnDisposeIsFalse()
        {
            TestTelemetryChannel testTelemetryChannel = new TestTelemetryChannel();

            using (ServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                       delegate { },
                       telemetryConfiguration => telemetryConfiguration.TelemetryChannel = testTelemetryChannel,
                       applicationInsightsOptions => applicationInsightsOptions.FlushOnDispose = false))
            {
                serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();
            }

            Assert.AreEqual(0, testTelemetryChannel.FlushCount);
        }
Esempio n. 6
0
        public void ApplicationInsightsLoggerLogsExceptionAsTraceWhenSwitchIsFalse()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => itemsReceived.Add(telemetryItem),
                configureTelemetryConfiguration: null,
                configureApplicationInsightsOptions: (appInsightsLoggerOptions) => appInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry = false);

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            testLogger.LogInformation("Testing");

            Exception trackingException = null;

            try
            {
                ThrowException();
            }
            catch (Exception ex)
            {
                trackingException = ex;
                testLogger.LogError(ex, "LoggerMessage");
            }

            Assert.IsInstanceOfType(itemsReceived[0], typeof(TraceTelemetry));
            Assert.IsInstanceOfType(itemsReceived[1], typeof(TraceTelemetry));

            Assert.AreEqual(SeverityLevel.Information, (itemsReceived[0] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual("Testing", (itemsReceived[0] as TraceTelemetry).Message);

            Assert.AreEqual(SeverityLevel.Error, (itemsReceived[1] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual("LoggerMessage", (itemsReceived[1] as TraceTelemetry).Message);

            Assert.IsTrue((itemsReceived[1] as TraceTelemetry).Properties["ExceptionMessage"].Contains("StackTraceEnabled"));

            Assert.IsTrue((itemsReceived[1] as TraceTelemetry).Properties.ContainsKey("ExceptionStackTrace"));

            Assert.AreEqual(
                trackingException.ToInvariantString(),
                (itemsReceived[1] as TraceTelemetry).Properties["ExceptionStackTrace"]);

            void ThrowException()
            {
                throw new Exception("StackTraceEnabled");
            }
        }
Esempio n. 7
0
        public void ApplicationInsightsLoggerIsInvokedWhenUsingILogger()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration((telemetryItem, telemetryProcessor) =>
            {
                itemsReceived.Add(telemetryItem);
            });

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            testLogger.LogInformation("Testing");
            testLogger.LogError(new Exception("ExceptionMessage"), "LoggerMessage");
            testLogger.LogInformation(new EventId(100, "TestEvent"), "TestingEvent");
            testLogger.LogCritical("Critical");
            testLogger.LogTrace("Trace");
            testLogger.LogWarning("Warning");
            testLogger.LogDebug("Debug");

            Assert.AreEqual(7, itemsReceived.Count);

            Assert.IsTrue((itemsReceived[2] as ISupportProperties).Properties.ContainsKey("EventId"));
            Assert.IsTrue((itemsReceived[2] as ISupportProperties).Properties.ContainsKey("EventName"));
            Assert.AreEqual("100", (itemsReceived[2] as ISupportProperties).Properties["EventId"]);
            Assert.AreEqual("TestEvent", (itemsReceived[2] as ISupportProperties).Properties["EventName"]);

            Assert.AreEqual("Microsoft.ApplicationInsights.ILoggerIntegrationTests", (itemsReceived[2] as ISupportProperties).Properties["CategoryName"]);
            Assert.AreEqual("Microsoft.ApplicationInsights.ILoggerIntegrationTests", (itemsReceived[0] as ISupportProperties).Properties["CategoryName"]);

            Assert.AreEqual(SeverityLevel.Information, (itemsReceived[0] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Error, (itemsReceived[1] as ExceptionTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Information, (itemsReceived[2] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Critical, (itemsReceived[3] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Verbose, (itemsReceived[4] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Warning, (itemsReceived[5] as TraceTelemetry).SeverityLevel);
            Assert.AreEqual(SeverityLevel.Verbose, (itemsReceived[6] as TraceTelemetry).SeverityLevel);

            Assert.AreEqual("Testing", (itemsReceived[0] as TraceTelemetry).Message);
            Assert.AreEqual("ExceptionMessage", (itemsReceived[1] as ExceptionTelemetry).Message);
            Assert.AreEqual("LoggerMessage", (itemsReceived[1] as ExceptionTelemetry).Properties["FormattedMessage"]);
            Assert.AreEqual("TestingEvent", (itemsReceived[2] as TraceTelemetry).Message);
            Assert.AreEqual("Critical", (itemsReceived[3] as TraceTelemetry).Message);
            Assert.AreEqual("Trace", (itemsReceived[4] as TraceTelemetry).Message);
            Assert.AreEqual("Warning", (itemsReceived[5] as TraceTelemetry).Message);
            Assert.AreEqual("Debug", (itemsReceived[6] as TraceTelemetry).Message);
        }
Esempio n. 8
0
        public void ApplicationInsightsLoggerPopulateStructureLoggingParamsIntoCustomPropertiesWhenScopeDisabled()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            // Disable scope
            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => itemsReceived.Add(telemetryItem),
                configureTelemetryConfiguration: null,
                configureApplicationInsightsOptions: (appInsightsLoggerOptions) => appInsightsLoggerOptions.IncludeScopes = false);

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            testLogger.LogInformation("Testing structured with {CustomerName} {Age}", "TestCustomerName", 20);

            Assert.AreEqual("Testing structured with TestCustomerName 20", (itemsReceived[0] as TraceTelemetry).Message);
            var customProperties = (itemsReceived[0] as TraceTelemetry).Properties;

            Assert.IsTrue(customProperties["CustomerName"].Equals("TestCustomerName"));
            Assert.IsTrue(customProperties["Age"].Equals("20"));
        }
        public void ApplicationInsightsLoggerLogsExceptionAsExceptionWhenSwitchIsTrue()
        {
            List <ITelemetry> itemsReceived = new List <ITelemetry>();

            // Case where Exceptions are logged as Exception Telemetry.
            IServiceProvider serviceProvider = ILoggerIntegrationTests.SetupApplicationInsightsLoggerIntegration(
                (telemetryItem, telemetryProcessor) => itemsReceived.Add(telemetryItem),
                configureTelemetryConfiguration: null,
                configureApplicationInsightsOptions: (appInsightsLoggerOptions) => appInsightsLoggerOptions.TrackExceptionsAsExceptionTelemetry = true);

            ILogger <ILoggerIntegrationTests> testLogger = serviceProvider.GetRequiredService <ILogger <ILoggerIntegrationTests> >();

            testLogger.LogInformation("Testing");
            testLogger.LogError(new Exception("TestException"), "Exception");

            Assert.IsInstanceOfType(itemsReceived[0], typeof(TraceTelemetry));
            Assert.IsInstanceOfType(itemsReceived[1], typeof(ExceptionTelemetry));

            Assert.AreEqual("Testing", (itemsReceived[0] as TraceTelemetry).Message);
            Assert.AreEqual("Exception", (itemsReceived[1] as ExceptionTelemetry).Message);
        }