added ILoggable interface and factored out duplicate code into interfaces; added comments
This commit is contained in:
parent
65937ec510
commit
dfc5c56a50
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace SimpleLog {
|
||||
|
||||
private DefaultLogger() {
|
||||
this.LogHandlers = new List<ILogHandler>();
|
||||
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
|
||||
/// <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>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a log handler with this logger
|
||||
/// </summary>
|
||||
/// <param name="handler">The log handler to register</param>
|
||||
public void RegisterLogHandler(ILogHandler handler) {
|
||||
this.LogHandlers.Add(handler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters the given log handler
|
||||
/// </summary>
|
||||
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
||||
public bool UnregisterLogHandler(ILogHandler handler) {
|
||||
if (this.LogHandlers.Contains(handler)) {
|
||||
this.LogHandlers.Remove(handler);
|
||||
@ -83,10 +69,6 @@ namespace SimpleLog {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all log handlers that match the given assembly name
|
||||
/// </summary>
|
||||
/// <returns>Number of log handlers that were unregistered</returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Gets or sets the global message handler
|
||||
/// </summary>
|
||||
public IMessageHandler MessageHandler {
|
||||
get {
|
||||
return this.messageHandler;
|
||||
@ -111,9 +88,6 @@ namespace SimpleLog {
|
||||
this.messageHandler = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets or sets the global log level
|
||||
/// </summary>
|
||||
public LogLevel LogLevel {
|
||||
get {
|
||||
return this.logLevel;
|
||||
@ -122,12 +96,6 @@ namespace SimpleLog {
|
||||
this.logLevel = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IMessageFormatter members
|
||||
/// <summary>
|
||||
/// Gets or sets the global date format
|
||||
/// </summary>
|
||||
public string DateFormat {
|
||||
get {
|
||||
return this.dateFormat;
|
||||
@ -136,9 +104,6 @@ namespace SimpleLog {
|
||||
this.dateFormat = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets or sets the global line terminator
|
||||
/// </summary>
|
||||
public string LineTerminator {
|
||||
get {
|
||||
return this.lineTerminator;
|
||||
|
@ -22,7 +22,7 @@ namespace SimpleLog {
|
||||
}
|
||||
|
||||
#region IMessageHandler Members
|
||||
public string ConstructLogMessage(string message, LogLevel level) {
|
||||
public string FormatLogMessage(string message, LogLevel level) {
|
||||
List<string> messageData = new List<string>();
|
||||
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
||||
messageData.Add(level.ToString().ToUpper());
|
||||
|
@ -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; }
|
||||
/// <summary>
|
||||
/// Gracefully shuts down the log handler
|
||||
/// </summary>
|
||||
void GracefulShutDown();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log level
|
||||
/// </summary>
|
||||
LogLevel? LogLevel { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
23
SimpleLog/ILoggable.cs
Normal file
23
SimpleLog/ILoggable.cs
Normal file
@ -0,0 +1,23 @@
|
||||
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,10 +1,29 @@
|
||||
namespace SimpleLog {
|
||||
public interface ILogger : IMessageFormatter {
|
||||
public interface ILogger : IMessageFormatter, ILoggable {
|
||||
|
||||
bool Log(object message, LogLevel level);
|
||||
/// <summary>
|
||||
/// Registers a log handler with this logger
|
||||
/// </summary>
|
||||
/// <param name="handler">The log handler to register</param>
|
||||
void RegisterLogHandler(ILogHandler handler);
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters the given log handler
|
||||
/// </summary>
|
||||
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
||||
bool UnregisterLogHandler(ILogHandler handler);
|
||||
|
||||
/// <summary>
|
||||
/// Removes all log handlers that match the given assembly name
|
||||
/// </summary>
|
||||
/// <returns>Number of log handlers that were unregistered</returns>
|
||||
int UnregisterLogHandlerType(string assemblyName);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log level
|
||||
/// </summary>
|
||||
LogLevel LogLevel {get; set;}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,14 @@
|
||||
namespace SimpleLog {
|
||||
public interface IMessageFormatter {
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date format
|
||||
/// </summary>
|
||||
string DateFormat { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the line terminator
|
||||
/// </summary>
|
||||
string LineTerminator { get; set; }
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,23 @@
|
||||
namespace SimpleLog {
|
||||
|
||||
public interface IMessageHandler : IMessageFormatter {
|
||||
|
||||
string ConstructLogMessage(string message, LogLevel level);
|
||||
/// <summary>
|
||||
/// Formats the log message
|
||||
/// </summary>
|
||||
/// <param name="message">Message to format</param>
|
||||
/// <param name="level">The level of the log message</param>
|
||||
/// <returns>The formatted log message</returns>
|
||||
string FormatLogMessage(string message, LogLevel level);
|
||||
|
||||
/// <summary>
|
||||
/// Converts the message object to a string
|
||||
/// </summary>
|
||||
string ConvertMessageToString(object message);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the custom context for this message handler
|
||||
/// </summary>
|
||||
string Context { get; set; }
|
||||
|
||||
}
|
||||
|
@ -16,11 +16,14 @@
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DefineConstants>
|
||||
</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>
|
||||
</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@ -47,6 +50,7 @@
|
||||
<ItemGroup>
|
||||
<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