예제 #1
0
        public void LogsCorrectUrl()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                Mock.Of <IFakeService>(),
                new LoggingServiceInterceptor(ctx, client));

            impl.TestServiceMethod();
            client.Flush();
            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            requestTelemetry.Should().NotBeNull();
            (requestTelemetry.Success ?? true).Should().BeTrue();
            requestTelemetry.Url.AbsoluteUri.Should().StartWith(ctx.ServiceName.AbsoluteUri);
            requestTelemetry.Url.AbsoluteUri.Should().Contain(nameof(IFakeService));
            telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().Should().BeEmpty();
        }
        public void InterceptCatchesExceptions()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);


            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var collection = new ServiceCollection();

            collection.AddSingleton(client);
            MockBuilder.RegisterStatelessServiceContext(collection);
            collection.AddSingleton(service.Object);
            ServiceProvider provider = collection.BuildServiceProvider();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                (object)null,
                new InvokeInNewScopeInterceptor <IFakeService>(provider));

            var ex = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <InvalidOperationException>().Which;

            ex.Message.Should().Be("Test Exception Text");

            client.Flush();

            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            requestTelemetry.Should().NotBeNull();
            requestTelemetry.Success.Should().BeFalse();
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            exceptionTelemetry.Should().NotBeNull();
            exceptionTelemetry.Exception.Should().BeSameAs(ex);
        }