Beispiel #1
0
        A_context_does_not_capture_LogEvents_outside_the_same_logical_call_context_even_when_they_run_concurrently()
        {
            var usingEnteredSignal = new ManualResetEvent(false);

            var loggingFinishedSignal = new ManualResetEvent(false);

            var contextGuid = Guid.Empty;

            var logTask = Task.Run(() =>
            {
                usingEnteredSignal.WaitOne();

                Log.Information("");

                loggingFinishedSignal.Set();
            });

            var logContextTask = Task.Run(() =>
            {
                using (var context = TestCorrelator.CreateContext())
                {
                    usingEnteredSignal.Set();
                    loggingFinishedSignal.WaitOne();
                    contextGuid = context.Guid;
                }
            });

            Task.WaitAll(logTask, logContextTask);

            TestCorrelator.GetLogEventsFromContextGuid(contextGuid)
            .Should().BeEmpty();
        }
Beispiel #2
0
        public void A_context_does_not_capture_LogEvents_outside_of_it()
        {
            Guid contextGuid;

            using (var context = TestCorrelator.CreateContext())
            {
                contextGuid = context.Guid;
            }

            Log.Information("");

            TestCorrelator.GetLogEventsFromContextGuid(contextGuid).Should().BeEmpty();
        }
Beispiel #3
0
        public void A_context_does_not_enrich_LogEvents_emitted_within_it()
        {
            using (var context = TestCorrelator.CreateContext())
            {
                Log.Information("");

                TestCorrelator.GetLogEventsFromContextGuid(context.Guid)
                .Should()
                .ContainSingle()
                .Which.Properties.Should()
                .BeEmpty();
            }
        }
Beispiel #4
0
        // ReSharper disable once MemberCanBeMadeStatic.Global
        public void LogServerTrace(ITestOutputHelper testOutputHelper)
        {
            if (_testCorrelatorContext == null)
            {
                return;
            }

            var logEvents = TestCorrelator.GetLogEventsFromContextGuid(_testCorrelatorContext.Guid).ToList();

            foreach (LogEvent logEvent in logEvents)
            {
                testOutputHelper.WriteLine(logEvent.RenderMessage());
            }
        }
Beispiel #5
0
        public void A_context_within_a_context_adds_an_additional_context_to_LogEvents()
        {
            using (var outerContext = TestCorrelator.CreateContext())
            {
                using (var innerContext = TestCorrelator.CreateContext())
                {
                    Log.Information("");

                    TestCorrelator.GetLogEventsFromContextGuid(innerContext.Guid)
                    .Should().ContainSingle();

                    TestCorrelator.GetLogEventsFromContextGuid(outerContext.Guid)
                    .Should().ContainSingle();
                }
            }
        }
Beispiel #6
0
        A_context_captures_LogEvents_inside_the_same_logical_call_context_even_when_they_are_in_tasks_started_outside_of_it()
        {
            Task logTask;
            Guid contextGuid;

            using (var context = TestCorrelator.CreateContext())
            {
                logTask = new Task(() => { Log.Information(""); });

                contextGuid = context.Guid;
            }

            logTask.Start();

            Task.WaitAll(logTask);

            TestCorrelator.GetLogEventsFromContextGuid(contextGuid).Should()
            .ContainSingle();
        }
Beispiel #7
0
        public void When_A_Request_Logger_Is_In_The_Pipeline()
        {
            Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.TestCorrelator().CreateLogger();
            using (var context = TestCorrelator.CreateContext())
            {
                var myCommand = new MyCommand();

                var registry = new SubscriberRegistry();
                registry.Register <MyCommand, IHandleRequests <MyCommand> >();
                var container = new ServiceCollection();


                var requestLogger = new RequestLoggingHandler <MyCommand>();

                container.AddTransient <IHandleRequests <MyCommand>, MyLoggedHandler>();
                container.AddTransient(typeof(RequestLoggingHandler <MyCommand>), provider => requestLogger);

                var handlerFactory = new ServiceProviderHandlerFactory(container.BuildServiceProvider());

                var commandProcessor = new CommandProcessor(registry, handlerFactory, handlerFactory,
                                                            new InMemoryRequestContextFactory(), new PolicyRegistry());


                commandProcessor.Send(myCommand);

                //_should_log_the_request_handler_call
                //_should_log_the_type_of_handler_in_the_call

                //TestCorrelator.GetLogEventsFromCurrentContext().Should().HaveCount(3);
                TestCorrelator.GetLogEventsFromContextGuid(context.Guid)
                .Should().Contain(x => x.MessageTemplate.Text.StartsWith("Logging handler pipeline call"))
                .Which.Properties["1"].ToString().Should().Be($"\"{typeof(MyCommand)}\"");


                commandProcessor?.Dispose();
            }
        }