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 += "\\[some kind of context\\] ";
|
||||||
expected += "log message\n";
|
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() {
|
private DefaultLogger() {
|
||||||
this.LogHandlers = new List<ILogHandler>();
|
this.LogHandlers = new List<ILogHandler>();
|
||||||
this.logLevel = LogLevel.Warning;
|
this.logLevel = SimpleLog.LogLevel.Warning;
|
||||||
this.lineTerminator = SimpleLog.LineTerminator.Unix;
|
this.lineTerminator = SimpleLog.LineTerminator.Unix;
|
||||||
this.Enabled = true;
|
this.Enabled = true;
|
||||||
this.messageHandler = new DefaultMessageHandler();
|
this.messageHandler = new DefaultMessageHandler();
|
||||||
@ -31,12 +31,6 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region ILogger members
|
#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) {
|
public bool Log(object message, LogLevel level) {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
@ -45,13 +39,13 @@ namespace SimpleLog {
|
|||||||
LogLevel allowedLevel;
|
LogLevel allowedLevel;
|
||||||
IMessageHandler messageHandler;
|
IMessageHandler messageHandler;
|
||||||
foreach (ILogHandler handler in this.LogHandlers) {
|
foreach (ILogHandler handler in this.LogHandlers) {
|
||||||
allowedLevel = handler.LogLevel ?? this.LogLevel;
|
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
|
||||||
if (level <= allowedLevel) {
|
if (level <= allowedLevel) {
|
||||||
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
||||||
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
||||||
|
|
||||||
convertedMessage = messageHandler.ConvertMessageToString(message);
|
convertedMessage = messageHandler.ConvertMessageToString(message);
|
||||||
convertedMessage = messageHandler.ConstructLogMessage(convertedMessage, level);
|
convertedMessage = messageHandler.FormatLogMessage(convertedMessage, level);
|
||||||
|
|
||||||
success = (success && handler.Log(convertedMessage, allowedLevel));
|
success = (success && handler.Log(convertedMessage, allowedLevel));
|
||||||
}
|
}
|
||||||
@ -61,18 +55,10 @@ namespace SimpleLog {
|
|||||||
return success;
|
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) {
|
public void RegisterLogHandler(ILogHandler handler) {
|
||||||
this.LogHandlers.Add(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) {
|
public bool UnregisterLogHandler(ILogHandler handler) {
|
||||||
if (this.LogHandlers.Contains(handler)) {
|
if (this.LogHandlers.Contains(handler)) {
|
||||||
this.LogHandlers.Remove(handler);
|
this.LogHandlers.Remove(handler);
|
||||||
@ -83,10 +69,6 @@ namespace SimpleLog {
|
|||||||
return false;
|
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) {
|
public int UnregisterLogHandlerType(string assemblyName) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
foreach (ILogHandler handler in this.LogHandlers) {
|
foreach (ILogHandler handler in this.LogHandlers) {
|
||||||
@ -97,12 +79,7 @@ namespace SimpleLog {
|
|||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Accessors
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the global message handler
|
|
||||||
/// </summary>
|
|
||||||
public IMessageHandler MessageHandler {
|
public IMessageHandler MessageHandler {
|
||||||
get {
|
get {
|
||||||
return this.messageHandler;
|
return this.messageHandler;
|
||||||
@ -111,9 +88,6 @@ namespace SimpleLog {
|
|||||||
this.messageHandler = value;
|
this.messageHandler = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the global log level
|
|
||||||
/// </summary>
|
|
||||||
public LogLevel LogLevel {
|
public LogLevel LogLevel {
|
||||||
get {
|
get {
|
||||||
return this.logLevel;
|
return this.logLevel;
|
||||||
@ -122,12 +96,6 @@ namespace SimpleLog {
|
|||||||
this.logLevel = value;
|
this.logLevel = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region IMessageFormatter members
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the global date format
|
|
||||||
/// </summary>
|
|
||||||
public string DateFormat {
|
public string DateFormat {
|
||||||
get {
|
get {
|
||||||
return this.dateFormat;
|
return this.dateFormat;
|
||||||
@ -136,9 +104,6 @@ namespace SimpleLog {
|
|||||||
this.dateFormat = value;
|
this.dateFormat = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the global line terminator
|
|
||||||
/// </summary>
|
|
||||||
public string LineTerminator {
|
public string LineTerminator {
|
||||||
get {
|
get {
|
||||||
return this.lineTerminator;
|
return this.lineTerminator;
|
||||||
|
@ -22,7 +22,7 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region IMessageHandler Members
|
#region IMessageHandler Members
|
||||||
public string ConstructLogMessage(string message, LogLevel level) {
|
public string FormatLogMessage(string message, LogLevel level) {
|
||||||
List<string> messageData = new List<string>();
|
List<string> messageData = new List<string>();
|
||||||
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
||||||
messageData.Add(level.ToString().ToUpper());
|
messageData.Add(level.ToString().ToUpper());
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
public interface ILogHandler {
|
public interface ILogHandler : ILoggable {
|
||||||
|
|
||||||
bool Log(object message, LogLevel level);
|
/// <summary>
|
||||||
LogLevel? LogLevel { get; set; }
|
/// Gracefully shuts down the log handler
|
||||||
IMessageHandler MessageHandler { get; set; }
|
/// </summary>
|
||||||
void GracefulShutDown();
|
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 {
|
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);
|
void RegisterLogHandler(ILogHandler handler);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unregisters the given log handler
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
||||||
bool UnregisterLogHandler(ILogHandler handler);
|
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);
|
int UnregisterLogHandlerType(string assemblyName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the log level
|
||||||
|
/// </summary>
|
||||||
|
LogLevel LogLevel {get; set;}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
public interface IMessageFormatter {
|
public interface IMessageFormatter {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the date format
|
||||||
|
/// </summary>
|
||||||
string DateFormat { get; set; }
|
string DateFormat { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the line terminator
|
||||||
|
/// </summary>
|
||||||
string LineTerminator { get; set; }
|
string LineTerminator { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,23 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
|
|
||||||
public interface IMessageHandler : IMessageFormatter {
|
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);
|
string ConvertMessageToString(object message);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the custom context for this message handler
|
||||||
|
/// </summary>
|
||||||
string Context { get; set; }
|
string Context { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,14 @@
|
|||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>true</Optimize>
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>
|
||||||
|
</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
|
<DocumentationFile>
|
||||||
|
</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>pdbonly</DebugType>
|
||||||
@ -47,6 +50,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="DefaultLogger.cs" />
|
<Compile Include="DefaultLogger.cs" />
|
||||||
<Compile Include="DefaultMessageHandler.cs" />
|
<Compile Include="DefaultMessageHandler.cs" />
|
||||||
|
<Compile Include="ILoggable.cs" />
|
||||||
<Compile Include="ILogger.cs" />
|
<Compile Include="ILogger.cs" />
|
||||||
<Compile Include="IMessageFormatter.cs" />
|
<Compile Include="IMessageFormatter.cs" />
|
||||||
<Compile Include="ILogHandler.cs" />
|
<Compile Include="ILogHandler.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user