diff --git a/SimpleLog.Tests/ConsoleLogHandlerTest.cs b/SimpleLog.Tests/ConsoleLogHandlerTest.cs new file mode 100644 index 0000000..85b94ce --- /dev/null +++ b/SimpleLog.Tests/ConsoleLogHandlerTest.cs @@ -0,0 +1,39 @@ +using System; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace SimpleLog.Tests { + [TestFixture] + public class ConsoleLogHandlerTest { + + private ConsoleLogHandler handler; + + [SetUp] + public void Init() { + handler = new ConsoleLogHandler(); + } + + [Test] + public void TestLog() { + Assert.That(this.handler.Log("yay", LogLevel.Critical)); + Assert.That(this.handler.Log("yay", LogLevel.Debug)); + } + + [Test] + public void TestGetOutputStream() { + Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Warning), Is.SameAs(Console.Error)); + Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Error), Is.SameAs(Console.Error)); + Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Critical), Is.SameAs(Console.Error)); + + Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Debug), Is.SameAs(Console.Out)); + Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Info), Is.SameAs(Console.Out)); + } + + [Test] + public void TestAccessors() { + Assert.That(handler.MessageHandler, Is.TypeOf(typeof(DefaultMessageHandler))); + Assert.That(handler.LogLevel, Is.EqualTo(null)); + } + + } +} diff --git a/SimpleLog.Tests/SimpleLog.Tests.csproj b/SimpleLog.Tests/SimpleLog.Tests.csproj index 7fccfa2..486f498 100644 --- a/SimpleLog.Tests/SimpleLog.Tests.csproj +++ b/SimpleLog.Tests/SimpleLog.Tests.csproj @@ -48,6 +48,7 @@ + diff --git a/SimpleLog/ConsoleLogHandler.cs b/SimpleLog/ConsoleLogHandler.cs new file mode 100644 index 0000000..228e4be --- /dev/null +++ b/SimpleLog/ConsoleLogHandler.cs @@ -0,0 +1,56 @@ +using System; + +namespace SimpleLog { + public class ConsoleLogHandler : ILogHandler { + + protected IMessageHandler messageHandler; + protected LogLevel? logLevel; + + public ConsoleLogHandler() { + this.messageHandler = new DefaultMessageHandler(); + this.logLevel = null; + } + + #region ILogHandler Members + public void GracefulShutDown() { + throw new NotImplementedException(); + } + + public LogLevel? LogLevel { + get { + return this.logLevel; + } + set { + this.logLevel = value; + } + } + + public bool Log(string message, LogLevel level) { + System.IO.TextWriter outputStream = ConsoleLogHandler.GetOutputStream(level); + outputStream.Write(message); + return true; + } + + /// + /// Gets the output stream for the specified log level + /// + /// Console.Out or Console.Error + public static System.IO.TextWriter GetOutputStream(LogLevel level) { + if (level >= SimpleLog.LogLevel.Warning) { + return Console.Error; + } + + return Console.Out; + } + + public IMessageHandler MessageHandler { + get { + return this.messageHandler; + } + set { + this.messageHandler = value; + } + } + #endregion + } +} diff --git a/SimpleLog/DefaultLogger.cs b/SimpleLog/DefaultLogger.cs index 3201054..fe75884 100644 --- a/SimpleLog/DefaultLogger.cs +++ b/SimpleLog/DefaultLogger.cs @@ -47,7 +47,7 @@ namespace SimpleLog { convertedMessage = messageHandler.ConvertMessageToString(message); convertedMessage = messageHandler.FormatLogMessage(convertedMessage, level); - success = (success && handler.Log(convertedMessage, allowedLevel)); + success = (success && handler.Log(convertedMessage, level)); } } } diff --git a/SimpleLog/ILogHandler.cs b/SimpleLog/ILogHandler.cs index 178a828..e8cc11b 100644 --- a/SimpleLog/ILogHandler.cs +++ b/SimpleLog/ILogHandler.cs @@ -1,5 +1,5 @@ namespace SimpleLog { - public interface ILogHandler : ILoggable { + public interface ILogHandler { /// /// Gracefully shuts down the log handler @@ -11,5 +11,18 @@ /// LogLevel? LogLevel { get; set; } + /// + /// Logs a message at the specified log level + /// + /// The message to log + /// The log level of the message + /// TRUE if all handler succesfully logged, or false if any of them failed + bool Log(string message, LogLevel level); + + /// + /// Gets or sets the message handler + /// + IMessageHandler MessageHandler { get; set; } + } } diff --git a/SimpleLog/ILoggable.cs b/SimpleLog/ILoggable.cs deleted file mode 100644 index 5b51cc3..0000000 --- a/SimpleLog/ILoggable.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace SimpleLog { - public interface ILoggable { - - /// - /// Logs a message at the specified log level - /// - /// The message to log - /// The log level of the message - /// TRUE if all handler succesfully logged, or false if any of them failed - bool Log(object message, LogLevel level); - - /// - /// Gets or sets the message handler - /// - IMessageHandler MessageHandler { get; set; } - - } -} diff --git a/SimpleLog/ILogger.cs b/SimpleLog/ILogger.cs index fb25f4b..0e696a8 100644 --- a/SimpleLog/ILogger.cs +++ b/SimpleLog/ILogger.cs @@ -1,5 +1,5 @@ namespace SimpleLog { - public interface ILogger : IMessageFormatter, ILoggable { + public interface ILogger : IMessageFormatter { /// /// Registers a log handler with this logger @@ -24,6 +24,18 @@ /// LogLevel LogLevel {get; set;} + /// + /// Logs a message at the specified log level + /// + /// The message to log + /// The log level of the message + /// TRUE if all handler succesfully logged, or false if any of them failed + bool Log(object message, LogLevel level); + + /// + /// Gets or sets the message handler + /// + IMessageHandler MessageHandler { get; set; } } } diff --git a/SimpleLog/SimpleLog.csproj b/SimpleLog/SimpleLog.csproj index ea29989..277008d 100644 --- a/SimpleLog/SimpleLog.csproj +++ b/SimpleLog/SimpleLog.csproj @@ -48,9 +48,9 @@ + -