private static void Log500(Exception ex)
        {
            var logger = new ApplicationInsightsLogger("main-webpage", new TelemetryClient(), new ApplicationInsightsLoggerOptions()
            {
                IncludeScopes = false
            });

            logger.LogError(ex, "Error in the application");
        }
Пример #2
0
        public void TestLoggerWithNegativeFilterDoesNotCreateExceptionTelemetryOnLoggedError()
        {
            bool            trackedTelemetry = false;
            TelemetryClient client           = CommonMocks.MockTelemetryClient((t) => trackedTelemetry = true);

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(false); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);

            Assert.False(trackedTelemetry);
        }
        public void TestLoggerCreatesExceptionTelemetryOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
            });

            ILogger logger    = new ApplicationInsightsLogger("test", client, (s, l) => { return(true); });
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);
        }
Пример #4
0
        public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWhenTrackExceptionsAsExceptionTelemetryIsSetToTrue()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;

                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
            });

            ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return(true); }, new ApplicationInsightsLoggerOptions {
                TrackExceptionsAsExceptionTelemetry = true
            });
            var exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);
        }
Пример #5
0
        public void TestLoggerCreatesExceptionWithoutEventIdTelemetryOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);

                Assert.False(exceptionTelemetry.Properties.ContainsKey("EventId"));
                Assert.False(exceptionTelemetry.Properties.ContainsKey("EventName"));
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(new EventId(22, "TestEvent"), exception, "Error: " + exception.Message);
        }
Пример #6
0
        public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWithParameters()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error, Value: 123", exceptionTelemetry.Message);

                Assert.Equal("This is an error", exceptionTelemetry.Properties["ex"]);
                Assert.Equal("123", exceptionTelemetry.Properties["value"]);
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: {ex}, Value: {value}", exception.Message, 123);
        }
Пример #7
0
        public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithEventIdOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is an error", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Error, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);

                Assert.Equal("22", traceTelemetry.Properties["EventId"]);
                Assert.Equal("TestEvent", traceTelemetry.Properties["EventName"]);
            });
            var options = new ApplicationInsightsLoggerOptions {
                IncludeEventId = true
            };

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, options);

            logger.LogError(new EventId(22, "TestEvent"), "This is an error");
        }
Пример #8
0
        public void TestLoggerAdditionalExceptionDataIsAddedToTelemetry()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error, Value: 123", exceptionTelemetry.Message);

                Assert.Equal("test", exceptionTelemetry.Properties["AdditionalProperty1"]);
                Assert.Equal("test2", exceptionTelemetry.Properties["AdditionalProperty2"]);
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            exception.Data.Add("AdditionalProperty1", "test");
            exception.Data.Add("AdditionalProperty2", "test2");
            logger.LogError(0, exception, "Error: {ex}, Value: {value}", exception.Message, 123);
        }
Пример #9
0
        public void TestLoggerIncludingEventIdCreatesExceptionWithEventIdTelemetryOnLoggedErrorWithParameters()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: ERROR This is an error", exceptionTelemetry.Message);

                Assert.Equal("22", exceptionTelemetry.Properties["EventId"]);
                Assert.Equal("ERROR", exceptionTelemetry.Properties["EventName"]);
                Assert.Equal("This is an error", exceptionTelemetry.Properties["Message"]);
            });
            var options = new ApplicationInsightsLoggerOptions {
                IncludeEventId = true
            };

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, options);
            var     exception = new Exception("This is an error");

            logger.LogError(new EventId(22, "TestEvent"), exception, "Error: {EventName} {Message}", "ERROR", exception.Message);
        }