From dfc5c56a50649482d4e2d1818fc3fd3c26facb24 Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 11 Apr 2009 23:25:33 +0000 Subject: [PATCH] added ILoggable interface and factored out duplicate code into interfaces; added comments --- SimpleLog.Tests/DefaultMessageHandlerTest.cs | 2 +- SimpleLog/DefaultLogger.cs | 41 ++------------------ SimpleLog/DefaultMessageHandler.cs | 2 +- SimpleLog/ILogHandler.cs | 13 +++++-- SimpleLog/ILoggable.cs | 23 +++++++++++ SimpleLog/ILogger.cs | 23 ++++++++++- SimpleLog/IMessageFormatter.cs | 7 ++++ SimpleLog/IMessageHandler.cs | 17 +++++++- SimpleLog/SimpleLog.csproj | 8 +++- 9 files changed, 87 insertions(+), 49 deletions(-) create mode 100644 SimpleLog/ILoggable.cs diff --git a/SimpleLog.Tests/DefaultMessageHandlerTest.cs b/SimpleLog.Tests/DefaultMessageHandlerTest.cs index 49f6dad..16bed41 100644 --- a/SimpleLog.Tests/DefaultMessageHandlerTest.cs +++ b/SimpleLog.Tests/DefaultMessageHandlerTest.cs @@ -37,7 +37,7 @@ namespace SimpleLog.Tests { expected += "\\[some kind of context\\] "; expected += "log message\n"; - Assert.That(this.messageHandler.ConstructLogMessage("log message", LogLevel.Warning), Text.Matches(expected)); + Assert.That(this.messageHandler.FormatLogMessage("log message", LogLevel.Warning), Text.Matches(expected)); } } diff --git a/SimpleLog/DefaultLogger.cs b/SimpleLog/DefaultLogger.cs index 84c311a..3201054 100644 --- a/SimpleLog/DefaultLogger.cs +++ b/SimpleLog/DefaultLogger.cs @@ -23,7 +23,7 @@ namespace SimpleLog { private DefaultLogger() { this.LogHandlers = new List(); - this.logLevel = LogLevel.Warning; + this.logLevel = SimpleLog.LogLevel.Warning; this.lineTerminator = SimpleLog.LineTerminator.Unix; this.Enabled = true; this.messageHandler = new DefaultMessageHandler(); @@ -31,12 +31,6 @@ namespace SimpleLog { } #region ILogger members - /// - /// 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 public bool Log(object message, LogLevel level) { bool success = true; @@ -45,13 +39,13 @@ namespace SimpleLog { LogLevel allowedLevel; IMessageHandler messageHandler; foreach (ILogHandler handler in this.LogHandlers) { - allowedLevel = handler.LogLevel ?? this.LogLevel; + allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel; if (level <= allowedLevel) { messageHandler = handler.MessageHandler ?? this.MessageHandler; messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat; convertedMessage = messageHandler.ConvertMessageToString(message); - convertedMessage = messageHandler.ConstructLogMessage(convertedMessage, level); + convertedMessage = messageHandler.FormatLogMessage(convertedMessage, level); success = (success && handler.Log(convertedMessage, allowedLevel)); } @@ -61,18 +55,10 @@ namespace SimpleLog { return success; } - /// - /// Registers a log handler with this logger - /// - /// The log handler to register public void RegisterLogHandler(ILogHandler handler) { this.LogHandlers.Add(handler); } - /// - /// Unregisters the given log handler - /// - /// TRUE if successfully unregistered, FALSE if not public bool UnregisterLogHandler(ILogHandler handler) { if (this.LogHandlers.Contains(handler)) { this.LogHandlers.Remove(handler); @@ -83,10 +69,6 @@ namespace SimpleLog { return false; } - /// - /// Removes all log handlers that match the given assembly name - /// - /// Number of log handlers that were unregistered public int UnregisterLogHandlerType(string assemblyName) { int count = 0; foreach (ILogHandler handler in this.LogHandlers) { @@ -97,12 +79,7 @@ namespace SimpleLog { return count; } - #endregion - #region Accessors - /// - /// Gets or sets the global message handler - /// public IMessageHandler MessageHandler { get { return this.messageHandler; @@ -111,9 +88,6 @@ namespace SimpleLog { this.messageHandler = value; } } - /// - /// Gets or sets the global log level - /// public LogLevel LogLevel { get { return this.logLevel; @@ -122,12 +96,6 @@ namespace SimpleLog { this.logLevel = value; } } - #endregion - - #region IMessageFormatter members - /// - /// Gets or sets the global date format - /// public string DateFormat { get { return this.dateFormat; @@ -136,9 +104,6 @@ namespace SimpleLog { this.dateFormat = value; } } - /// - /// Gets or sets the global line terminator - /// public string LineTerminator { get { return this.lineTerminator; diff --git a/SimpleLog/DefaultMessageHandler.cs b/SimpleLog/DefaultMessageHandler.cs index 5f58fc5..21c1acf 100644 --- a/SimpleLog/DefaultMessageHandler.cs +++ b/SimpleLog/DefaultMessageHandler.cs @@ -22,7 +22,7 @@ namespace SimpleLog { } #region IMessageHandler Members - public string ConstructLogMessage(string message, LogLevel level) { + public string FormatLogMessage(string message, LogLevel level) { List messageData = new List(); messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now)); messageData.Add(level.ToString().ToUpper()); diff --git a/SimpleLog/ILogHandler.cs b/SimpleLog/ILogHandler.cs index 38da9e4..178a828 100644 --- a/SimpleLog/ILogHandler.cs +++ b/SimpleLog/ILogHandler.cs @@ -1,10 +1,15 @@ namespace SimpleLog { - public interface ILogHandler { + public interface ILogHandler : ILoggable { - bool Log(object message, LogLevel level); - LogLevel? LogLevel { get; set; } - IMessageHandler MessageHandler { get; set; } + /// + /// Gracefully shuts down the log handler + /// void GracefulShutDown(); + /// + /// Gets or sets the log level + /// + LogLevel? LogLevel { get; set; } + } } diff --git a/SimpleLog/ILoggable.cs b/SimpleLog/ILoggable.cs new file mode 100644 index 0000000..5b51cc3 --- /dev/null +++ b/SimpleLog/ILoggable.cs @@ -0,0 +1,23 @@ +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 2e48874..fb25f4b 100644 --- a/SimpleLog/ILogger.cs +++ b/SimpleLog/ILogger.cs @@ -1,10 +1,29 @@ namespace SimpleLog { - public interface ILogger : IMessageFormatter { + public interface ILogger : IMessageFormatter, ILoggable { - bool Log(object message, LogLevel level); + /// + /// Registers a log handler with this logger + /// + /// The log handler to register void RegisterLogHandler(ILogHandler handler); + + /// + /// Unregisters the given log handler + /// + /// TRUE if successfully unregistered, FALSE if not bool UnregisterLogHandler(ILogHandler handler); + + /// + /// Removes all log handlers that match the given assembly name + /// + /// Number of log handlers that were unregistered int UnregisterLogHandlerType(string assemblyName); + /// + /// Gets or sets the log level + /// + LogLevel LogLevel {get; set;} + + } } diff --git a/SimpleLog/IMessageFormatter.cs b/SimpleLog/IMessageFormatter.cs index b244eb8..1d26e28 100644 --- a/SimpleLog/IMessageFormatter.cs +++ b/SimpleLog/IMessageFormatter.cs @@ -1,7 +1,14 @@ namespace SimpleLog { public interface IMessageFormatter { + /// + /// Gets or sets the date format + /// string DateFormat { get; set; } + + /// + /// Gets or sets the line terminator + /// string LineTerminator { get; set; } } diff --git a/SimpleLog/IMessageHandler.cs b/SimpleLog/IMessageHandler.cs index 724ad42..57e0a34 100644 --- a/SimpleLog/IMessageHandler.cs +++ b/SimpleLog/IMessageHandler.cs @@ -1,8 +1,23 @@ namespace SimpleLog { + public interface IMessageHandler : IMessageFormatter { - string ConstructLogMessage(string message, LogLevel level); + /// + /// Formats the log message + /// + /// Message to format + /// The level of the log message + /// The formatted log message + string FormatLogMessage(string message, LogLevel level); + + /// + /// Converts the message object to a string + /// string ConvertMessageToString(object message); + + /// + /// Gets or sets the custom context for this message handler + /// string Context { get; set; } } diff --git a/SimpleLog/SimpleLog.csproj b/SimpleLog/SimpleLog.csproj index 6c18a3d..ea29989 100644 --- a/SimpleLog/SimpleLog.csproj +++ b/SimpleLog/SimpleLog.csproj @@ -16,11 +16,14 @@ true full - false + true bin\Debug\ - DEBUG;TRACE + + prompt 4 + + pdbonly @@ -47,6 +50,7 @@ +