コード例 #1
0
        public void LogTestFallback()
        {
            var expectedCode = System.Net.HttpStatusCode.InternalServerError;

            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage(expectedCode));

            httpClient = new HttpClient(handlerMock.Object)
            {
                BaseAddress = new Uri(expectedLoggingEndpoint)
            };

            var fakeLogger = new FakeLogger();

            serviceMonitoringLogger = new ServiceMonitoringLogger("Test", new ServiceMonitoringLoggerConfig(configuration.Object), httpClient, null, fakeLogger);

            serviceMonitoringLogger.Log(LogLevel.Warning, "Mega warning");

            Assert.True(fakeLogger.HasLogged);
            var expectedMessage = $"services_monitoring - {expectedApplicationName} - Http request failed with status {(int)expectedCode}";

            Assert.AreEqual(expectedMessage, fakeLogger.LoggedMessage);
        }
コード例 #2
0
        public void LogExceptionTest()
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock.Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(rm => rm.Method == HttpMethod.Post &&
                                               rm.RequestUri.Equals(expectedLoggingEndpoint) &&
                                               rm.Content.ReadAsStringAsync().Result.Contains("MissingMethodException") &&
                                               rm.Content.ReadAsStringAsync().Result.Contains("EmptyMessage")
                                               ),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.Created))
            .Verifiable();

            httpClient = new HttpClient(handlerMock.Object)
            {
                BaseAddress = new Uri(expectedLoggingEndpoint)
            };

            serviceMonitoringLogger = new ServiceMonitoringLogger("Test", new ServiceMonitoringLoggerConfig(configuration.Object), httpClient);

            serviceMonitoringLogger.Log(LogLevel.Warning, new MissingMethodException(), "EmptyMessage");
            Mock.Verify();
            Assert.Pass();
        }