public void CreateContextWithEnabledLoggerAndRequestIdCreatesActivityAndSetsActivityInScope() { // Arrange // Generate an id we can use for the request id header (in the correct format) var activity = new Activity("IncomingRequest"); activity.Start(); var id = activity.Id; activity.Stop(); var logger = new LoggerWithScopes(isEnabled: true); var hostingApplication = CreateApplication(out var features, logger: logger, configure: context => { context.Request.Headers["Request-Id"] = id; }); // Act var context = hostingApplication.CreateContext(features); Assert.Single(logger.Scopes); var pairs = ((IReadOnlyList <KeyValuePair <string, object> >)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value); Assert.Equal(Activity.Current.Id, pairs["SpanId"].ToString()); Assert.Equal(Activity.Current.RootId, pairs["TraceId"].ToString()); Assert.Equal(id, pairs["ParentId"].ToString()); }
public void CreateContextWithEnabledLoggerCreatesActivityAndSetsActivityIdInScope() { // Arrange var logger = new LoggerWithScopes(isEnabled: true); var hostingApplication = CreateApplication(out var features, logger: logger); // Act var context = hostingApplication.CreateContext(features); Assert.Single(logger.Scopes); var pairs = ((IReadOnlyList <KeyValuePair <string, object> >)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value); Assert.Equal(Activity.Current.Id, pairs["ActivityId"].ToString()); }
public void ActivityStopDoesNotFireIfNoListenerAttachedForStart() { // Arrange var diagnosticSource = new DiagnosticListener("DummySource"); var logger = new LoggerWithScopes(isEnabled: true); var hostingApplication = CreateApplication(out var features, diagnosticSource: diagnosticSource, logger: logger); var startFired = false; var stopFired = false; diagnosticSource.Subscribe(new CallbackDiagnosticListener(pair => { // This should not fire if (pair.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start") { startFired = true; } // This should not fire if (pair.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop") { stopFired = true; } }), (s, o, arg3) => { // The events are off return(false); }); // Act var context = hostingApplication.CreateContext(features); Assert.Single(logger.Scopes); var pairs = ((IReadOnlyList <KeyValuePair <string, object> >)logger.Scopes[0]).ToDictionary(p => p.Key, p => p.Value); Assert.Equal(Activity.Current.Id, pairs["ActivityId"].ToString()); hostingApplication.DisposeContext(context, exception: null); Assert.False(startFired); Assert.False(stopFired); Assert.Null(Activity.Current); }
public void ActivityStopDoesNotFireIfNoListenerAttachedForStart() { // Arrange var diagnosticListener = new DiagnosticListener("DummySource"); var logger = new LoggerWithScopes(isEnabled: true); var hostingApplication = CreateApplication(out var features, diagnosticListener: diagnosticListener, logger: logger); var startFired = false; var stopFired = false; diagnosticListener.Subscribe(new CallbackDiagnosticListener(pair => { // This should not fire if (pair.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start") { startFired = true; } // This should not fire if (pair.Key == "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop") { stopFired = true; } }), (s, o, arg3) => { // The events are off return(false); }); // Act var context = hostingApplication.CreateContext(features); hostingApplication.DisposeContext(context, exception: null); Assert.False(startFired); Assert.False(stopFired); Assert.Null(Activity.Current); }