got rid of ILoggable, added ConsoleLogHandler and test
This commit is contained in:
parent
dfc5c56a50
commit
ff35dd9d58
39
SimpleLog.Tests/ConsoleLogHandlerTest.cs
Normal file
39
SimpleLog.Tests/ConsoleLogHandlerTest.cs
Normal file
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -48,6 +48,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConsoleLogHandlerTest.cs" />
|
||||
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
56
SimpleLog/ConsoleLogHandler.cs
Normal file
56
SimpleLog/ConsoleLogHandler.cs
Normal file
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the output stream for the specified log level
|
||||
/// </summary>
|
||||
/// <returns>Console.Out or Console.Error</returns>
|
||||
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
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
namespace SimpleLog {
|
||||
public interface ILogHandler : ILoggable {
|
||||
public interface ILogHandler {
|
||||
|
||||
/// <summary>
|
||||
/// Gracefully shuts down the log handler
|
||||
@ -11,5 +11,18 @@
|
||||
/// </summary>
|
||||
LogLevel? LogLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Logs a message at the specified log level
|
||||
/// </summary>
|
||||
/// <param name="message">The message to log</param>
|
||||
/// <param name="level">The log level of the message</param>
|
||||
/// <returns>TRUE if all handler succesfully logged, or false if any of them failed</returns>
|
||||
bool Log(string message, LogLevel level);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message handler
|
||||
/// </summary>
|
||||
IMessageHandler MessageHandler { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SimpleLog {
|
||||
public interface ILoggable {
|
||||
|
||||
/// <summary>
|
||||
/// Logs a message at the specified log level
|
||||
/// </summary>
|
||||
/// <param name="message">The message to log</param>
|
||||
/// <param name="level">The log level of the message</param>
|
||||
/// <returns>TRUE if all handler succesfully logged, or false if any of them failed</returns>
|
||||
bool Log(object message, LogLevel level);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message handler
|
||||
/// </summary>
|
||||
IMessageHandler MessageHandler { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
namespace SimpleLog {
|
||||
public interface ILogger : IMessageFormatter, ILoggable {
|
||||
public interface ILogger : IMessageFormatter {
|
||||
|
||||
/// <summary>
|
||||
/// Registers a log handler with this logger
|
||||
@ -24,6 +24,18 @@
|
||||
/// </summary>
|
||||
LogLevel LogLevel {get; set;}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a message at the specified log level
|
||||
/// </summary>
|
||||
/// <param name="message">The message to log</param>
|
||||
/// <param name="level">The log level of the message</param>
|
||||
/// <returns>TRUE if all handler succesfully logged, or false if any of them failed</returns>
|
||||
bool Log(object message, LogLevel level);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message handler
|
||||
/// </summary>
|
||||
IMessageHandler MessageHandler { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +48,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConsoleLogHandler.cs" />
|
||||
<Compile Include="DefaultLogger.cs" />
|
||||
<Compile Include="DefaultMessageHandler.cs" />
|
||||
<Compile Include="ILoggable.cs" />
|
||||
<Compile Include="ILogger.cs" />
|
||||
<Compile Include="IMessageFormatter.cs" />
|
||||
<Compile Include="ILogHandler.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user