diff --git a/SimpleLog.Tests/DefaultLoggerTest.cs b/SimpleLog.Tests/DefaultLoggerTest.cs new file mode 100644 index 0000000..ecf2cd9 --- /dev/null +++ b/SimpleLog.Tests/DefaultLoggerTest.cs @@ -0,0 +1,110 @@ +using System.Collections.Generic; +using NUnit.Framework; +using NUnit.Mocks; +using NUnit.Framework.SyntaxHelpers; + +namespace SimpleLog.Tests { + [TestFixture] + public class DefaultLoggerTest { + + private DefaultLogger Logger; + private DynamicMock Handler; + + [SetUp] + public void Init() { + this.Logger = DefaultLogger.Instance; + this.Logger.Enabled = true; + this.Logger.MessageHandler = new DefaultMessageHandler(); + this.Handler = new DynamicMock(typeof(ILogHandler)); + + } + + [TearDown] + public void DeInit() { + this.Logger.UnregisterAllLogHandlers(); + this.Logger.Enabled = true; + this.Logger.MessageHandler = new DefaultMessageHandler(); + } + + [Test] + public void TestRegisterAndUnregisterLogHandler() { + int oldCount = this.Logger.LogHandlerCount; + this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler)); + + + ILogHandler handler = (ILogHandler)this.Handler.MockInstance; + + this.Logger.RegisterLogHandler(handler); + Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1)); + + Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.ILogHandler"), Is.EqualTo(1)); + + this.Logger.RegisterLogHandler(handler); + Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1)); + + this.Logger.UnregisterAllLogHandlers(); + Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0)); + } + + [Test] + public void TestLogWhenDisabled() { + this.Logger.Enabled = false; + Assert.That(this.Logger.Log("yay", LogLevel.Debug)); + } + + [Test] + public void TestLogWithHighLogLevel() { + DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler)); + msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay"); + msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning); + + //NUnit mock objects suck, so setting this property on DefaultLogger + //and assuming handler.MessageHandler returns null is the best I can do + this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance; + + this.Handler.ExpectAndReturn("Log", true, "foobar", LogLevel.Warning); + + this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance); + + Assert.That(this.Logger.Log("yay", LogLevel.Warning)); + this.Handler.Verify(); + msgHandler.Verify(); + } + + [Test] + public void TestLogWithHighLogLevelFails() { + DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler)); + msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay"); + msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning); + + //NUnit mock objects suck, so setting this property on DefaultLogger + //and assuming handler.MessageHandler returns null is the best I can do + this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance; + + this.Handler.ExpectAndReturn("Log", false, "foobar", LogLevel.Warning); + + this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance); + + Assert.That(!this.Logger.Log("yay", LogLevel.Warning)); + this.Handler.Verify(); + msgHandler.Verify(); + } + + [Test] + public void TestLogWithLowLogLevel() { + DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler)); + msgHandler.ExpectNoCall("ConvertMessageToString"); + msgHandler.ExpectNoCall("FormatLogMessage"); + this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance; + + this.Handler.ExpectNoCall("Log"); + + this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance); + + Assert.That(this.Logger.Log("yay", LogLevel.Debug)); + this.Handler.Verify(); + msgHandler.Verify(); + } + + } +} diff --git a/SimpleLog.Tests/SimpleLog.Tests.csproj b/SimpleLog.Tests/SimpleLog.Tests.csproj index 486f498..9e9fed8 100644 --- a/SimpleLog.Tests/SimpleLog.Tests.csproj +++ b/SimpleLog.Tests/SimpleLog.Tests.csproj @@ -49,6 +49,7 @@ + diff --git a/SimpleLog/DefaultLogger.cs b/SimpleLog/DefaultLogger.cs index 3665df4..7153b59 100644 --- a/SimpleLog/DefaultLogger.cs +++ b/SimpleLog/DefaultLogger.cs @@ -40,7 +40,7 @@ namespace SimpleLog { IMessageHandler messageHandler; foreach (ILogHandler handler in this.LogHandlers) { allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel; - if (level <= allowedLevel) { + if (level >= allowedLevel) { messageHandler = handler.MessageHandler ?? this.MessageHandler; messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat; @@ -59,24 +59,18 @@ namespace SimpleLog { this.LogHandlers.Add(handler); } - public bool UnregisterLogHandler(ILogHandler handler) { - if (this.LogHandlers.Contains(handler)) { - this.LogHandlers.Remove(handler); - handler.GracefulShutDown(); - return true; - } - - return false; + public int UnregisterLogHandlerType(string qualifiedClassName) { + return this.LogHandlers.RemoveAll( + delegate(ILogHandler handler) { + //Console.WriteLine(handler.GetType().ToString() == qualifiedClassName); + return (handler.GetType().ToString() == qualifiedClassName); + } + ); } - public int UnregisterLogHandlerType(string assemblyName) { - int count = 0; - foreach (ILogHandler handler in this.LogHandlers) { - if (handler.GetType().AssemblyQualifiedName == assemblyName) { - count += this.UnregisterLogHandler(handler) ? 1 : 0; - } - } - + public int UnregisterAllLogHandlers() { + int count = this.LogHandlerCount; + this.LogHandlers.RemoveRange(0, count); return count; } @@ -118,5 +112,14 @@ namespace SimpleLog { } #endregion + /// + /// Gets the number of registered log handlers + /// + public int LogHandlerCount { + get { + return this.LogHandlers.Count; + } + } + } } diff --git a/SimpleLog/ILogger.cs b/SimpleLog/ILogger.cs index 0e696a8..7ab0b4e 100644 --- a/SimpleLog/ILogger.cs +++ b/SimpleLog/ILogger.cs @@ -11,7 +11,7 @@ /// Unregisters the given log handler /// /// TRUE if successfully unregistered, FALSE if not - bool UnregisterLogHandler(ILogHandler handler); + //bool UnregisterLogHandler(ILogHandler handler); /// /// Removes all log handlers that match the given assembly name @@ -19,6 +19,12 @@ /// Number of log handlers that were unregistered int UnregisterLogHandlerType(string assemblyName); + /// + /// Unregisters all registered log handlers + /// + /// Number of log handlers that were unregistered + int UnregisterAllLogHandlers(); + /// /// Gets or sets the log level ///