Beispiel #1
0
        public async Task LogsExceptionAsync()
        {
            IHost host = null;

            try
            {
                host = DefaultHostBuilder.CreateHostBuilder().Build();
                await host.StartAsync();

                IContextExceptionLogger exceptionLogger = host.Services.GetRequiredService <IContextExceptionLogger>();
                try
                {
                    ThrowsException();
                }
                catch (Exception e)
                {
                    exceptionLogger.Log(e, null);
                }

                var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.Default, _testId);
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(
                    errorEvent, _testId, nameof(ThrowsException), verifyHttpContext: false);
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
Beispiel #2
0
        public async Task LogsMultipleExceptions()
        {
            var requestTask1 = _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}");
            var requestTask2 = _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsArgumentException)}/{_testId}");
            var requestTask3 = _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}");
            var requestTask4 = _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}");

            var response = await requestTask1;

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            response = await requestTask2;
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            response = await requestTask3;
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            response = await requestTask4;
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var errorEvents = ErrorEventEntryVerifiers.VerifyMany(s_polling, _testId, 4);

            var exceptionEvents = errorEvents
                                  .Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsException)));

            Assert.Equal(3, exceptionEvents.Count());
            foreach (var errorEvent in exceptionEvents)
            {
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));
            }

            var argumentExceptionEvent = errorEvents
                                         .Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsArgumentException)))
                                         .Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(argumentExceptionEvent, _testId, nameof(ErrorReportingController.ThrowsArgumentException));
        }
Beispiel #3
0
        public async Task LogsMultipleExceptions()
        {
            await Assert.ThrowsAsync <Exception>(() =>
                                                 _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            await Assert.ThrowsAsync <ArgumentException>(() =>
                                                         _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsArgumentException)}/{_testId}"));

            await Assert.ThrowsAsync <Exception>(() =>
                                                 _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            await Assert.ThrowsAsync <Exception>(() =>
                                                 _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            var errorEvents = ErrorEventEntryVerifiers.VerifyMany(s_polling, _testId, 4);

            var exceptionEvents = errorEvents.Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsException)));

            Assert.Equal(3, exceptionEvents.Count());
            foreach (var errorEvent in exceptionEvents)
            {
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));
            }

            var argumentExceptionEvent = errorEvents.Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsArgumentException))).Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(argumentExceptionEvent, _testId, nameof(ErrorReportingController.ThrowsArgumentException));
        }
        public async Task LogsMultipleExceptions()
        {
            using var server = GetTestServer <ErrorReportingTestApplication>();
            using var client = server.CreateClient();

            await Assert.ThrowsAsync <Exception>(() =>
                                                 client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}_0"));

            await Assert.ThrowsAsync <ArgumentException>(() =>
                                                         client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsArgumentException)}/{_testId}"));

            await Assert.ThrowsAsync <Exception>(() =>
                                                 client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}_1"));

            await Assert.ThrowsAsync <Exception>(() =>
                                                 client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}_2"));

            _fixture.AddValidator(_testId, results =>
            {
                Assert.Equal(4, results.Count);
                Assert.All(results, AssertTraceContext);

                var errorEvents = ErrorEventEntryVerifiers.VerifyMany(ErrorEventEntryPolling.NoRetry, _testId, 4);

                var exceptionEvents = errorEvents.Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsException))).ToList();
                Assert.Equal(3, exceptionEvents.Count);
                Assert.All(exceptionEvents, e => ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(e, _testId, nameof(ErrorReportingController.ThrowsException)));

                var argumentExceptionEvent = errorEvents.Where(e => e.Message.Contains(nameof(ErrorReportingController.ThrowsArgumentException))).Single();
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(argumentExceptionEvent, _testId, nameof(ErrorReportingController.ThrowsArgumentException));
            });
        }
Beispiel #5
0
        public async Task Logs_UnhandledException()
        {
            await Assert.ThrowsAsync <Exception>(() => _client.GetAsync($"/ErrorLoggingSamples/{nameof(ErrorLoggingSamplesController.ThrowsException)}/{_testId}"));

            var errorEvent = s_errorPolling.GetEvents(_startTime, _testId, 1).Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorLoggingSamplesController.ThrowsException), (int)HttpStatusCode.OK);
        }
        public async Task Logs_UnhandledException()
        {
            await Assert.ThrowsAsync <Exception>(() => _client.GetAsync($"/ErrorLoggingSamples/{nameof(ErrorLoggingSamplesController.ThrowsException)}/{_testId}"));

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_errorPolling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorLoggingSamplesController.ThrowsException));
        }
Beispiel #7
0
        private static async Task TestErrorReporting(string testId, HttpClient client, bool verifyServiceAndVersion = true)
        {
            await Assert.ThrowsAsync <Exception>(() => client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{testId}"));

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.Default, testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, testId, nameof(ErrorReportingController.ThrowsException), verifyServiceAndVersion: verifyServiceAndVersion);
        }
        private static async Task TestErrorReporting(string testId, HttpClient client)
        {
            var polling = new ErrorEventEntryPolling();
            await Assert.ThrowsAsync <Exception>(() => client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{testId}"));

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(polling, testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, testId, nameof(ErrorReportingController.ThrowsException));
        }
Beispiel #9
0
        public async Task LogsException()
        {
            await Assert.ThrowsAsync <Exception>(() =>
                                                 _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));
        }
Beispiel #10
0
        public async Task ManualLog()
        {
            var response = await _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowCatchLog)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowCatchLog));
        }
Beispiel #11
0
        public async Task LogsThrownInHttpMessageHandler()
        {
            var response = await _client.GetAsync($"handler/{_testId}");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var errorEvent = s_polling.GetEvents(_startTime, _testId, 1).Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, "SendAsync");
        }
Beispiel #12
0
        public async Task ManualLog_GoogleWebApiLogger()
        {
            var response = await _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowCatchWithGoogleWebApiLogger)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var errorEvent = s_polling.GetEvents(_startTime, _testId, 1).Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowCatchWithGoogleWebApiLogger));
        }
        public async Task ManualLog_GoogleWebApiLogger()
        {
            var response = await _client.GetAsync(
                $"api/Home/{nameof(HomeController.CatchesAndLogsExceptions)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(
                errorEvent, _testId, "DoSomething");
        }
        public async Task LogsException()
        {
            var response = await _client.GetAsync(
                $"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}");

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(
                errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));
        }
        public async Task Logs_Explicitly()
        {
            var response = await _client.GetAsync($"/ErrorLoggingSamples/{nameof(ErrorLoggingSamplesController.CatchesAndLogsException)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            // Verifying with function name ThrowsExceptions because that is the function
            // that actually throws the Exception so will be the one included as
            // ErrorEvent.Context.ReportLocation.FunctionName
            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorLoggingSamplesController.ThrowsException));
        }
        public async Task LogsException()
        {
            await Assert.ThrowsAsync <Exception>(() =>
                                                 _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));

            _fixture.AddValidator(_testId, results =>
            {
                var entry = Assert.Single(results);
                AssertTraceContext(entry);
            });
        }
        public async Task ManualLog()
        {
            var response = await _client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowCatchLog)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var errorEvent = ErrorEventEntryVerifiers.VerifySingle(s_polling, _testId);

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowCatchLog));

            _fixture.AddValidator(_testId, results =>
            {
                var entry = Assert.Single(results);
                AssertTraceContext(entry);
            });
        }
        public async Task LogsException()
        {
            using var server = GetTestServer <ErrorReportingTestApplication>();
            using var client = server.CreateClient();

            await Assert.ThrowsAsync <Exception>(() =>
                                                 client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}"));

            _fixture.AddValidator(_testId, results =>
            {
                var entry = Assert.Single(results);
                AssertTraceContext(entry);

                var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.NoRetry, _testId);
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));
            });
        }
Beispiel #19
0
        public async Task LogsException()
        {
            var response = await _client.GetAsync($"api/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{_testId}");

            var contentTask = response.Content.ReadAsStringAsync();

            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

            var errorEvent = s_polling.GetEvents(_startTime, _testId, 1).Single();

            ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowsException));

            var content = await contentTask;

            Assert.Contains(nameof(ErrorReportingController.ThrowsException), content);
            Assert.Contains(_testId, content);
        }
        public async Task ManualLog()
        {
            using var server = GetTestServer <ErrorReportingTestApplication>();
            using var client = server.CreateClient();

            var response = await client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowCatchLog)}/{_testId}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            _fixture.AddValidator(_testId, results =>
            {
                var entry = Assert.Single(results);
                AssertTraceContext(entry);

                var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.NoRetry, _testId);
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(errorEvent, _testId, nameof(ErrorReportingController.ThrowCatchLog));
            });
        }
        public async Task LogsExceptionAsync()
        {
            IHost host = null;
            // Hides that we use different classes so that we can have multiple CreateHostBuilder methods.
            Func <IHostBuilder> CreateHostBuilder = DefaultHostBuilder.CreateHostBuilder;

            try
            {
                // Sample: Start
                host = CreateHostBuilder().Build();
                await host.StartAsync();

                // End sample

                // Sample: LogException
                IContextExceptionLogger exceptionLogger = host.Services.GetRequiredService <IContextExceptionLogger>();
                try
                {
                    // This method call throws an exception.
                    ThrowsException();
                }
                catch (Exception e)
                {
                    // The logger may receive an IContextWrapper as a second parameter
                    // with information about the HTTP operation, if any, that produced
                    // the error. It may be null in which case it will be ignored.
                    exceptionLogger.Log(e, null);
                }
                // End sample

                var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.Default, _testId);
                ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(
                    errorEvent, _testId, nameof(ThrowsException), verifyHttpContext: false);
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }