public void Write_WhenLoggingIsEnabled_WritesToStringResult()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var environmentVariableReader = CreateEnvironmentVariableReaderMock(
                    isLoggingEnabled: true,
                    testDirectory: testDirectory);
                RandomLogMessageWithTime logMessage;
                DateTimeOffset           loggerInitLoggedAt;
                DateTimeOffset           randomMessageLoggedAt;
                using (var logger = new PluginLogger(environmentVariableReader.Object))
                {
                    loggerInitLoggedAt = DateTimeOffset.UtcNow;
                    Thread.Sleep(10000); // Enough for potential accuracy issues to arise.
                    logMessage            = new RandomLogMessageWithTime(logger.Now);
                    randomMessageLoggedAt = DateTimeOffset.UtcNow;
                    logger.Write(logMessage);
                }

                var logFile = GetLogFile(testDirectory);

                Assert.NotNull(logFile);
                Assert.True(logFile.Exists);

                var actualLines = File.ReadAllLines(logFile.FullName);

                Assert.Collection(actualLines,
                                  actualLine =>
                {
                    var message = VerifyOuterMessageAndReturnInnerMessage(actualLine, loggerInitLoggedAt.AddSeconds(-1), loggerInitLoggedAt.AddSeconds(1), "stopwatch");

                    Assert.Equal(1, message.Count);
                    Assert.Equal(Stopwatch.Frequency, message["frequency"].Value <long>());
                },
                                  actualLine =>
                {
                    var message = VerifyOuterMessageAndReturnInnerMessage(actualLine, randomMessageLoggedAt.AddSeconds(-1), randomMessageLoggedAt.AddSeconds(1), "random");

                    Assert.Equal(1, message.Count);
                    Assert.Equal(logMessage.Message, message["message"]);
                });

                environmentVariableReader.VerifyAll();
            }
        }
        public void Write_WhenLoggingIsNotEnabled_DoesNotCreateLogFile()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var environmentVariableReader = CreateEnvironmentVariableReaderMock(
                    isLoggingEnabled: false,
                    testDirectory: testDirectory);
                var logMessage = new RandomLogMessage();

                using (var logger = new PluginLogger(environmentVariableReader.Object))
                {
                    logger.Write(logMessage);
                }

                var files = Directory.GetFiles(testDirectory.Path, "*", SearchOption.TopDirectoryOnly);

                Assert.Empty(files);

                environmentVariableReader.VerifyAll();
            }
        }
Ejemplo n.º 3
0
        public void Write_WhenLoggingIsEnabled_WritesToStringResult()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var environmentVariableReader = CreateEnvironmentVariableReaderMock(
                    isLoggingEnabled: true,
                    testDirectory: testDirectory);
                var logMessage = new RandomLogMessage();

                using (var logger = new PluginLogger(environmentVariableReader.Object))
                {
                    logger.Write(logMessage);
                }

                var logFile = GetLogFile(testDirectory);

                Assert.NotNull(logFile);
                Assert.True(logFile.Exists);

                var actualLines = File.ReadAllLines(logFile.FullName);

                Assert.Collection(actualLines,
                                  actualLine =>
                {
                    var now = DateTimeOffset.UtcNow;

                    var message = VerifyOuterMessageAndReturnInnerMessage(actualLine, now.AddMinutes(-1), now.AddSeconds(1), "stopwatch");

                    Assert.Equal(1, message.Count);
                    Assert.Equal(Stopwatch.Frequency, message["frequency"].Value <long>());
                },
                                  actualLine => Assert.Equal(logMessage.Message, actualLine));

                environmentVariableReader.VerifyAll();
            }
        }