示例#1
0
        static void Main(string[] args)
        {
            var logsDirectory          = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "logs"));
            var rollingFileSink        = new RollingFileSink(logsDirectory, RollingStyle.Date);
            var logStatementDispatcher = new AsyncLogStatementDispatcher(new List <ISink> {
                rollingFileSink
            });
            var logger = new Logger(logStatementDispatcher);

            logger.LogDebug("Test Debug message");
            logger.LogInformation("Test Information message");
            logger.LogWarning("Test Warning message");
            logger.LogError("Test Error message");

            logger.Flush();
        }
示例#2
0
        public void IsLoggingAsync()
        {
            // Arrange
            var delay = 1000;
            var maxTimeToWriteToLogInMilliseconds = delay;
            var sink = new SimulatedSlowSink(delay);
            var logStatementDispathcer = new AsyncLogStatementDispatcher(new List <ISink> {
                sink
            });

            var sw = Stopwatch.StartNew();

            // Act
            var logger = new Logger(logStatementDispathcer);

            logger.Log("Message");

            // Assert
            Assert.Less(sw.ElapsedMilliseconds, maxTimeToWriteToLogInMilliseconds);
        }
        public void LogStatementIsWrittenToAllSinks()
        {
            // Arrange
            var firstSinkMock  = new Mock <ISink>(MockBehavior.Strict);
            var secondSinkMock = new Mock <ISink>(MockBehavior.Strict);
            var thirdSinkMock  = new Mock <ISink>(MockBehavior.Strict);
            var dispatcher     = new AsyncLogStatementDispatcher(new List <ISink> {
                firstSinkMock.Object, secondSinkMock.Object, thirdSinkMock.Object
            });

            firstSinkMock.Setup(sink => sink.Write(It.IsAny <string>()));
            secondSinkMock.Setup(sink => sink.Write(It.IsAny <string>()));
            thirdSinkMock.Setup(sink => sink.Write(It.IsAny <string>()));

            var logStatement = new LogStatement(DateTimeOffset.UtcNow, LogLevel.Information, "Message");

            // Act
            dispatcher.Dispatch(logStatement);

            // Assert
            firstSinkMock.Verify(sink => sink.Write(logStatement.Render()), Times.Once);
            secondSinkMock.Verify(sink => sink.Write(logStatement.Render()), Times.Once);
            thirdSinkMock.Verify(sink => sink.Write(logStatement.Render()), Times.Once);
        }