diff --git a/src/LogLibrary.Tests/LoggerFixture.cs b/src/LogLibrary.Tests/LoggerFixture.cs index 3dc2a31..d502894 100644 --- a/src/LogLibrary.Tests/LoggerFixture.cs +++ b/src/LogLibrary.Tests/LoggerFixture.cs @@ -1,63 +1,63 @@ -using NSubstitute; -using NUnit.Framework; - -#pragma warning disable S2699 // Tests should include assertions - -namespace LogLibrary.Tests -{ - [TestFixture] - public class LoggerFixture - { - private Logger _sut; - ILogDestination _logDestination; - - [SetUp] - public void SetUp() - { - _logDestination = Substitute.For(); - } - - private void CreateLogger(LogLevel level) - { - _sut = new Logger(level, _logDestination); - } - - [Test] - public void Verify_debug_log_not_logged_if_level_is_info() - { - CreateLogger(LogLevel.Info); - _sut.Log(LogLevel.Debug, "This should not be logged"); - _logDestination.DidNotReceive().AddLog(Arg.Any()); - } - - [Test] - public void Verify_info_log_is_logged_if_level_is_info() - { - CreateLogger(LogLevel.Info); - _sut.Log(LogLevel.Info, "This should not be logged"); - _logDestination.Received().AddLog(Arg.Any()); - } - +using NSubstitute; +using NUnit.Framework; + +#pragma warning disable S2699 // Tests should include assertions + +namespace LogLibrary.Tests +{ + [TestFixture] + public class LoggerFixture + { + private Logger _sut; + private ILogDestination _logDestination; + + [SetUp] + public void SetUp() + { + _logDestination = Substitute.For(); + } + + private void CreateLogger(LogLevel level) + { + _sut = new Logger(level, _logDestination); + } + + [Test] + public void Verify_debug_log_not_logged_if_level_is_info() + { + CreateLogger(LogLevel.Info); + _sut.Log(LogLevel.Debug, "This should not be logged"); + _logDestination.DidNotReceive().AddLog(Arg.Any()); + } + + [Test] + public void Verify_info_log_is_logged_if_level_is_info() + { + CreateLogger(LogLevel.Info); + _sut.Log(LogLevel.Info, "This should not be logged"); + _logDestination.Received().AddLog(Arg.Any()); + } + [Test] public void Verify_warn_log_is_logged_if_level_is_info() - { - CreateLogger(LogLevel.Info); - _sut.Log(LogLevel.Warn, "This should not be logged"); - _logDestination.Received().AddLog(Arg.Any()); - } - - [Test] - public void Verify_log_formatting() - { - CreateLogger(LogLevel.Info); - _sut.Log(LogLevel.Info, "This have a parameter {0}", "value"); + { + CreateLogger(LogLevel.Info); + _sut.Log(LogLevel.Warn, "This should not be logged"); + _logDestination.Received().AddLog(Arg.Any()); + } + + [Test] + public void Verify_log_formatting() + { + CreateLogger(LogLevel.Info); + _sut.Log(LogLevel.Info, "This have a parameter {0}", "value"); _logDestination.Received().AddLog(Arg.Do(m => - { - Assert.That(m.RenderedMessage, Is.EqualTo("This have a parameter value")); - })); - } - } -} - -#pragma warning restore S2699 // Tests should include assertions + { + Assert.That(m.RenderedMessage, Is.EqualTo("This have a parameter value")); + })); + } + } +} + +#pragma warning restore S2699 // Tests should include assertions diff --git a/src/LogLibrary/FileLogDestination.cs b/src/LogLibrary/FileLogDestination.cs new file mode 100644 index 0000000..f183a4e --- /dev/null +++ b/src/LogLibrary/FileLogDestination.cs @@ -0,0 +1,21 @@ +using System; +using System.IO; + +namespace LogLibrary +{ + public class FileLogDestination : ILogDestination + { + private readonly StreamWriter _streamWriter; + + public FileLogDestination(String fileName) + { + var _fileStream = new FileStream(fileName, FileMode.OpenOrCreate); + _fileStream.Seek(0, SeekOrigin.End); + _streamWriter = new StreamWriter(_fileStream); + } + public void AddLog(LogMessage message) + { + _streamWriter.Write($"{message.Level}[{message.Timestamp}] - {message.RenderedMessage}"); + } + } +} diff --git a/src/LogLibrary/LogLibrary.csproj b/src/LogLibrary/LogLibrary.csproj index c235666..c3e77fd 100644 --- a/src/LogLibrary/LogLibrary.csproj +++ b/src/LogLibrary/LogLibrary.csproj @@ -45,6 +45,7 @@ +