public async Task ExecuteAndTraceConsumerActionAsync_EnterAndExitLogged(bool mustFail) { var consumer = _serviceProvider.GetRequiredService <TestBroker>() .AddConsumer(TestConsumerEndpoint.GetDefault()); var expectedEnterMessage = $"Enter A 42 True | consumerId: {consumer.Id}, endpointName: test"; var expectedExitMessage = $"Exit A 42 True | consumerId: {consumer.Id}, endpointName: test"; var executed = false; Func <Task> act = () => _silverbackLogger.ExecuteAndTraceConsumerActionAsync( consumer, () => { _loggerSubstitute.Received(LogLevel.Trace, null, expectedEnterMessage, 1999); executed = true; if (mustFail) { throw new InvalidCastException(); } return(Task.CompletedTask); }, "Enter {string} {int} {bool}", "Exit {string} {int} {bool}", () => new object[] { "A", 42, true }); if (mustFail) { await act.Should().ThrowAsync <InvalidCastException>(); } else { await act.Should().NotThrowAsync(); } _loggerSubstitute.Received( LogLevel.Trace, mustFail ? typeof(InvalidCastException) : null, expectedExitMessage, 1999); executed.Should().BeTrue(); }