public void HttpExceptionsContentIsTruncatedByRichTelemetry() { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUnit"); ServiceCollection services = new ServiceCollection(); var channel = new FakeChannel(); services.AddSingleton <ITelemetryChannel>(channel); ServiceHost.ConfigureDefaultServices(services); var provider = services.BuildServiceProvider(); var client = provider.GetRequiredService <TelemetryClient>(); string bigContent = "Test content" + new string('*', 10000); var httpOperationException = new HttpOperationException { Response = new HttpResponseMessageWrapper( new HttpResponseMessage((HttpStatusCode)418), bigContent), }; client.TrackException(httpOperationException); client.Flush(); var exceptionTelemetries = channel.Telemetry.OfType <ExceptionTelemetry>().ToList(); Assert.Single(exceptionTelemetries); var exceptionTelemetry = exceptionTelemetries[0]; Assert.True(exceptionTelemetry.Properties.TryGetValue("responseText", out var responseText)); Assert.StartsWith("Test content", responseText); Assert.True(responseText.Length < bigContent.Length, "responseText.Length < bigContent.Length"); }
public void AreDependenciesRegistered() { Environment.SetEnvironmentVariable("ENVIRONMENT", Environments.Development); var config = new ConfigurationBuilder(); var collection = new ServiceCollection(); // The only scenario we are worried about is when running in the ServiceHost ServiceHost.ConfigureDefaultServices(collection); collection.AddSingleton <IConfiguration>(config.Build()); collection.AddSingleton <Startup>(); using ServiceProvider provider = collection.BuildServiceProvider(); var startup = provider.GetRequiredService <Startup>(); IEnumerable <Type> controllerTypes = typeof(Startup).Assembly.ExportedTypes .Where(t => typeof(ControllerBase).IsAssignableFrom(t)); Assert.True(DependencyInjectionValidation.IsDependencyResolutionCoherent( s => { foreach (ServiceDescriptor descriptor in collection) { s.Add(descriptor); } startup.ConfigureServices(s); }, out string message, additionalScopedTypes: controllerTypes), message); }
public void HttpExceptionsAreAugmentedByRichTelemetry() { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUnit"); ServiceCollection services = new ServiceCollection(); var channel = new FakeChannel(); services.AddSingleton <ITelemetryChannel>(channel); ServiceHost.ConfigureDefaultServices(services); var provider = services.BuildServiceProvider(); var client = provider.GetRequiredService <TelemetryClient>(); var httpOperationException = new HttpOperationException { Response = new HttpResponseMessageWrapper( new HttpResponseMessage((HttpStatusCode)418), "Test content"), }; client.TrackException(httpOperationException); client.Flush(); var exceptionTelemetries = channel.Telemetry.OfType <ExceptionTelemetry>().ToList(); exceptionTelemetries.Should().ContainSingle(); var exceptionTelemetry = exceptionTelemetries[0]; exceptionTelemetry.Properties.TryGetValue("statusCode", out var statusCodeTest).Should().BeTrue(); statusCodeTest.Should().Be("418"); exceptionTelemetry.Properties.TryGetValue("responseText", out var responseText).Should().BeTrue(); responseText.Should().Be("Test content"); }
public void AreDependenciesRegistered() { DependencyInjectionValidation.IsDependencyResolutionCoherent(s => { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT"); ServiceHost.ConfigureDefaultServices(s); Program.Configure(s); }, out string message).Should().BeTrue(message); }
public void AreDependenciesRegistered() { Assert.True(DependencyInjectionValidation.IsDependencyResolutionCoherent(s => { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT"); ServiceHost.ConfigureDefaultServices(s); Program.Configure(s); s.AddScoped <FeedCleanerService>(); }, out string message), message); }
public void AreDependenciesRegistered() { DependencyInjectionValidation.IsDependencyResolutionCoherent(s => { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT"); ServiceHost.ConfigureDefaultServices(s); Program.Configure(s); // The "IReliableStateManager" is provided by stateful services s.AddSingleton(Mock.Of <IReliableStateManager>()); s.AddScoped <DependencyUpdater>(); }, out string message).Should().BeTrue(message); }
public void AreDependenciesRegistered() { DependencyInjectionValidation.IsDependencyResolutionCoherent( s => { Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT"); ServiceHost.ConfigureDefaultServices(s); Program.Configure(s); }, out string message, additionalScopedTypes: new[] { typeof(CoreHealthMonitorService) } ) .Should() .BeTrue(message); }
private static TestData Setup() { Environment.SetEnvironmentVariable("ENVIRONMENT", Environments.Development); var channel = new Mock <ITelemetryChannel>(); var telemetry = new List <ITelemetry>(); channel.Setup(s => s.Send(Capture.In(telemetry))); var config = new ConfigurationBuilder(); var collection = new ServiceCollection(); collection.AddSingleton(channel.Object); collection.AddSingleton <OperationManager>(); collection.AddScoped <TrackedDisposable>(); // The only scenario we are worried about is when running in the ServiceHost ServiceHost.ConfigureDefaultServices(collection); collection.AddSingleton <IConfiguration>(config.Build()); collection.AddSingleton <Startup>(); ServiceProvider outerProvider = collection.BuildServiceProvider(); var startup = outerProvider.GetRequiredService <Startup>(); startup.ConfigureServices(collection); ServiceProvider innerProvider = collection.BuildServiceProvider(); var logger = innerProvider.GetRequiredService <ILogger <DependencyRegistrationTests> >(); var operations = innerProvider.GetRequiredService <OperationManager>(); var tc = innerProvider.GetRequiredService <TelemetryClient>(); Activity.Current = null; // AppInsights behaves very oddly if the ActivityId is W3C // It's not ideal for a test to mess with static state, but we need to ensure this works correctly Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical; return(new TestData(logger, operations, tc, telemetry, innerProvider, outerProvider)); }
public void LoggingWithScopes() { Environment.SetEnvironmentVariable("ENVIRONMENT", Environments.Development); Mock <ITelemetryChannel> channel = new Mock <ITelemetryChannel>(); List <ITelemetry> telemetry = new List <ITelemetry>(); channel.Setup(s => s.Send(Capture.In(telemetry))); var config = new ConfigurationBuilder(); var collection = new ServiceCollection(); collection.AddSingleton(channel.Object); collection.AddSingleton <OperationManager>(); // The only scenario we are worried about is when running in the ServiceHost ServiceHost.ConfigureDefaultServices(collection); collection.AddSingleton <IConfiguration>(config.Build()); collection.AddSingleton <Startup>(); using ServiceProvider outerProvider = collection.BuildServiceProvider(); var startup = outerProvider.GetRequiredService <Startup>(); startup.ConfigureServices(collection); using ServiceProvider innerProvider = collection.BuildServiceProvider(); var logger = innerProvider.GetRequiredService <ILogger <DependencyRegistrationTests> >(); var operations = innerProvider.GetRequiredService <OperationManager>(); var tc = innerProvider.GetRequiredService <TelemetryClient>(); Activity.Current = null; // AppInsights behaves very oddly if the ActivityId is W3C // It's not ideal for a test to mess with static state, but we need to ensure this works correctly Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical; using (tc.StartOperation <RequestTelemetry>("Fake operation")) { logger.LogError("Outside"); using (operations.BeginOperation("TEST-SCOPE:{TEST_KEY}", "TEST_VALUE")) { logger.LogError("Something: {TEST_SOMETHING_KEY}", "TEST_SOMETHING_VALUE"); logger.LogError("Else"); } logger.LogError("Outside again"); } innerProvider.GetRequiredService <TelemetryClient>().Flush(); List <TraceTelemetry> traces = telemetry.OfType <TraceTelemetry>().ToList(); Assert.Equal(4, traces.Count); { // The operation id should stay constant, it's the root string[] opIds = traces.Select(t => t.Context?.Operation?.Id).ToArray(); Assert.NotNull(opIds[0]); Assert.Equal(opIds[0], opIds[1]); Assert.Equal(opIds[1], opIds[2]); Assert.Equal(opIds[2], opIds[3]); } { // The parent ids should flow with the operation start/stop var parentIds = traces.Select(t => t.Context?.Operation?.ParentId).ToArray(); Assert.NotNull(parentIds[0]); Assert.NotEqual(parentIds[0], parentIds[1]); Assert.StartsWith(parentIds[0], parentIds[1]); Assert.Equal(parentIds[1], parentIds[2]); Assert.NotEqual(parentIds[2], parentIds[3]); Assert.Equal(parentIds[0], parentIds[3]); } // The things in the operation should flow the properties from the BeginOperation Assert.Equal("TEST_VALUE", traces[1].Properties.GetValueOrDefault("TEST_KEY")); // The things outside the operation should not have those properties Assert.DoesNotContain("TEST_VALUE", traces[3].Properties); }