使用 xunit 和 .net core 3.1 进行单元测试时让记录器退出

Getting logger out in while doing unit test using xunit and .net core 3.1

在输出 window 中执行 运行 我的单元测试时,我试图从记录器获取输出,但没有任何显示。我在测试中使用 ILoggingFactory class。我期待从测试 class 和实际实现 class 中看到我的所有日​​志,即 JobQueueManager。

Test.cs

public class ScheduledJobDatesTest :IClassFixture<ScheduleJobTestFixture>
    {
        private ScheduleJobTestFixture ScheduleJobFixtureHelper { get; }

        Mock<IDbConnection> mockJobQueueManager = new Mock<IDbConnection>();
        ILoggerFactory logger = new LoggerFactory();

        public ScheduledJobDatesTest(ScheduleJobTestFixture scheduleJobFixture)
        {
            ScheduleJobFixtureHelper = scheduleJobFixture;

            mockJobQueueManager.SetReturnsDefault(new SqlConnection());
            logger = LoggerFactory.Create(a => a.AddConsole());
            logger.CreateLogger("Executing Test");
        }

        [Fact]
        public void ValidScheduledJobDateForNextWeeklyAppointment()
        {
            Debug.WriteLine("Testing");
            logger.CreateLogger("Test").LogInformation("Test Information");
            //Arrange
            JobQueueManager jobQueueManager = new JobQueueManager(mockJobQueueManager.Object, logger.CreateLogger<JobQueueManager>());
            var scheduledJob = ScheduleJobFixtureHelper.GetMockedScheduledJob(JobType.Week);
            //Act
            bool isTrue = jobQueueManager.IsScheduledJobDateValidForJobType(scheduledJob.JobDate);
            //Assert
            Assert.True(isTrue);
        }

}

JobQueueManager.cs

public class JobQueueManager : IJobQueueManager
    {
        public IDbConnection DBConnection { get; }
        public ILogger<JobQueueManager> Logger { get; }

        public JobQueueManager(IDbConnection dbConnection, ILogger<JobQueueManager> logger)
        {
            DBConnection = dbConnection;
            Logger = logger;
            Logger.LogInformation("Initialized Job Queue Manager");
        }

    public bool IsScheduledJobDateValidForJobType(DateTime ScheduledJobDate)
        {
            Logger.LogDebug("Verifying  unit test");
            var currentDate = DateTime.Now;
            var nextJobDate = ScheduledJobDate;
            if (nextJobDate > currentDate)
            {
                var appointmentsDaysDifference = Math.Ceiling((nextJobDate - currentDate).TotalDays);
                if(appointmentsDaysDifference == 1)
                return true;
            }
            return false;
        }
}

您使用的是什么版本的 xUnit?

If you used xUnit.net 1.x, you may have previously been writing output to Console, Debug, or Trace. When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources. Users who are porting code from v1.x to v2.x should use one of the two new methods instead.

Source

似乎有很多方法可以解决这个问题,如果是我,我会考虑实施