示例#1
0
        public async Task LogIoTHubDependency_SinksToApplicationInsights_ResultsIEventHubsDependencyTelemetry()
        {
            // Arrange
            string componentName = BogusGenerator.Commerce.ProductName();
            string iotHubName    = BogusGenerator.Commerce.ProductName();

            using (ILoggerFactory loggerFactory = CreateLoggerFactory(config => config.Enrich.WithComponentName(componentName)))
            {
                ILogger logger = loggerFactory.CreateLogger <ApplicationInsightsSinkTests>();

                bool           isSuccessful = BogusGenerator.PickRandom(true, false);
                DateTimeOffset startTime    = BogusGenerator.Date.RecentOffset(days: 0);
                TimeSpan       duration     = BogusGenerator.Date.Timespan();
                Dictionary <string, object> telemetryContext = CreateTestTelemetryContext();

                // Act
                logger.LogIotHubDependency(iotHubName, isSuccessful, startTime, duration, telemetryContext);
            }

            // Assert
            using (ApplicationInsightsDataClient client = CreateApplicationInsightsClient())
            {
                await RetryAssertUntilTelemetryShouldBeAvailableAsync(async() =>
                {
                    EventsResults <EventsDependencyResult> results = await client.Events.GetDependencyEventsAsync(ApplicationId);
                    Assert.NotEmpty(results.Value);
                    Assert.Contains(results.Value, result =>
                    {
                        return(result.Dependency.Type == "Azure IoT Hub" &&
                               result.Dependency.Target == iotHubName &&
                               result.Cloud.RoleName == componentName);
                    });
                });
            }
        }
示例#2
0
        public void LogIoTHubDependency_WithTableStorageDependency_CreatesDependencyTelemetry()
        {
            // Arrange
            var     spySink          = new InMemoryLogSink();
            string  operationId      = $"operation-id-{Guid.NewGuid()}";
            ILogger logger           = CreateLogger(spySink, config => config.Enrich.WithProperty(ContextProperties.Correlation.OperationId, operationId));
            string  iotHubName       = _bogusGenerator.Commerce.ProductName();
            var     startTime        = DateTimeOffset.UtcNow;
            var     duration         = TimeSpan.FromSeconds(5);
            var     telemetryContext = new Dictionary <string, object>
            {
                ["DeviceName"] = "Sensor #102"
            };

            logger.LogIotHubDependency(iotHubName: iotHubName, isSuccessful: true, startTime: startTime, duration: duration, context: telemetryContext);
            LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);

            Assert.NotNull(logEvent);

            var converter = ApplicationInsightsTelemetryConverter.Create();

            // Act
            IEnumerable <ITelemetry> telemetries = converter.Convert(logEvent, formatProvider: null);

            // Assert
            AssertDoesNotContainLogProperty(logEvent, DependencyTracking.DependencyLogEntry);
            Assert.Collection(telemetries, telemetry =>
            {
                var dependencyTelemetry = Assert.IsType <DependencyTelemetry>(telemetry);
                Assert.Equal("Azure IoT Hub", dependencyTelemetry.Type);
                Assert.Equal(iotHubName, dependencyTelemetry.Target);
                Assert.Equal(startTime, dependencyTelemetry.Timestamp);
                Assert.Equal(duration, dependencyTelemetry.Duration);
                Assert.True(dependencyTelemetry.Success);

                AssertContainsTelemetryProperty(dependencyTelemetry, "DeviceName", "Sensor #102");
            });
        }