branch reintegration from 1.1
svn merge http://svn.tommymontgomery.com/SimpleLog/trunk http://svn.tommymontgomery.com/SimpleLog/branches/1.1
This commit is contained in:
parent
09ebd0e0f6
commit
f44b12cf56
@ -16,8 +16,8 @@ namespace SimpleLog.Tests {
|
|||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestLog() {
|
public void TestLog() {
|
||||||
Assert.That(this.handler.Log("yay", LogLevel.Critical));
|
Assert.That(this.handler.Log("yay", Framework.LogLevel.Critical));
|
||||||
Assert.That(this.handler.Log("yay", LogLevel.Debug));
|
Assert.That(this.handler.Log("yay", Framework.LogLevel.Debug));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using NUnit.Mocks;
|
using NUnit.Mocks;
|
||||||
using NUnit.Framework.SyntaxHelpers;
|
using NUnit.Framework.SyntaxHelpers;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog.Tests {
|
namespace SimpleLog.Tests {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
@ -12,8 +13,7 @@ namespace SimpleLog.Tests {
|
|||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Init() {
|
public void Init() {
|
||||||
this.Logger = DefaultLogger.Instance;
|
this.Logger = new DefaultLogger();
|
||||||
this.Logger.Enabled = true;
|
|
||||||
this.Logger.MessageHandler = new DefaultMessageHandler();
|
this.Logger.MessageHandler = new DefaultMessageHandler();
|
||||||
this.Handler = new DynamicMock(typeof(ILogHandler));
|
this.Handler = new DynamicMock(typeof(ILogHandler));
|
||||||
|
|
||||||
@ -28,19 +28,21 @@ namespace SimpleLog.Tests {
|
|||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestRegisterAndUnregisterLogHandler() {
|
public void TestRegisterAndUnregisterLogHandler() {
|
||||||
int oldCount = this.Logger.LogHandlerCount;
|
|
||||||
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
||||||
|
//this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
||||||
|
|
||||||
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
|
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
|
||||||
|
|
||||||
this.Logger.RegisterLogHandler(handler);
|
this.Logger.RegisterLogHandler(handler);
|
||||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1));
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
|
||||||
|
|
||||||
Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.ILogHandler"), Is.EqualTo(1));
|
//this.Logger.UnregisterLogHandlerType("SimpleLog.Framework.ILogHandler");
|
||||||
|
|
||||||
|
Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.Framework.ILogHandler"), Is.EqualTo(1));
|
||||||
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
||||||
|
|
||||||
this.Logger.RegisterLogHandler(handler);
|
this.Logger.RegisterLogHandler(handler);
|
||||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1));
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
|
||||||
|
|
||||||
this.Logger.UnregisterAllLogHandlers();
|
this.Logger.UnregisterAllLogHandlers();
|
||||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
||||||
|
@ -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.FormatLogMessage("log message", LogLevel.Warning), Text.Matches(expected));
|
Assert.That(this.messageHandler.FormatLogMessage("log message", Framework.LogLevel.Warning), Text.Matches(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
24
SimpleLog.Tests/LoggerManagerTest.cs
Normal file
24
SimpleLog.Tests/LoggerManagerTest.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using NUnit.Framework;
|
||||||
|
using NUnit.Framework.SyntaxHelpers;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
|
namespace SimpleLog.Tests {
|
||||||
|
[TestFixture]
|
||||||
|
public class LoggerManagerTest {
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestGetLogger() {
|
||||||
|
DefaultLogger logger = LoggerManager.GetLogger<DefaultLogger>();
|
||||||
|
Assert.That(logger, Is.TypeOf(typeof(DefaultLogger)));
|
||||||
|
DefaultLogger logger2 = LoggerManager.GetLogger<DefaultLogger>();
|
||||||
|
Assert.That(logger2, Is.TypeOf(typeof(DefaultLogger)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[ExpectedException("System.ArgumentException")]
|
||||||
|
public void TestGetLoggerThrowsArgumentException() {
|
||||||
|
LoggerManager.GetLogger<object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -51,6 +51,7 @@
|
|||||||
<Compile Include="ConsoleLogHandlerTest.cs" />
|
<Compile Include="ConsoleLogHandlerTest.cs" />
|
||||||
<Compile Include="DefaultLoggerTest.cs" />
|
<Compile Include="DefaultLoggerTest.cs" />
|
||||||
<Compile Include="DefaultMessageHandlerTest.cs" />
|
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||||
|
<Compile Include="LoggerManagerTest.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
// General Information about an assembly is controlled through the following
|
|
||||||
// set of attributes. Change these attribute values to modify the information
|
|
||||||
// associated with an assembly.
|
|
||||||
[assembly: AssemblyTitle("SimpleLog")]
|
|
||||||
[assembly: AssemblyDescription("Simple .NET 3.5 logging utility")]
|
|
||||||
[assembly: AssemblyConfiguration("")]
|
|
||||||
[assembly: AssemblyCompany("")]
|
|
||||||
[assembly: AssemblyProduct("SimpleLog")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © Tommy Montgomery 2009")]
|
|
||||||
[assembly: AssemblyTrademark("")]
|
|
||||||
[assembly: AssemblyCulture("")]
|
|
||||||
|
|
||||||
// Setting ComVisible to false makes the types in this assembly not visible
|
|
||||||
// to COM components. If you need to access a type in this assembly from
|
|
||||||
// COM, set the ComVisible attribute to true on that type.
|
|
||||||
[assembly: ComVisible(false)]
|
|
||||||
|
|
||||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
|
||||||
[assembly: Guid("eec4e17e-cbc6-4982-86dd-a3d0ce488eaf")]
|
|
||||||
|
|
||||||
// Version information for an assembly consists of the following four values:
|
|
||||||
//
|
|
||||||
// Major Version
|
|
||||||
// Minor Version
|
|
||||||
// Build Number
|
|
||||||
// Revision
|
|
||||||
//
|
|
||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
|
||||||
// by using the '*' as shown below:
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -26,10 +26,11 @@
|
|||||||
</DocumentationFile>
|
</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>None</DebugType>
|
||||||
<Optimize>true</Optimize>
|
<Optimize>true</Optimize>
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
<DefineConstants>
|
||||||
|
</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
@ -48,24 +49,21 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="DefaultLogger.cs" />
|
<Compile Include="Src\DefaultLogger.cs" />
|
||||||
<Compile Include="DefaultMessageHandler.cs" />
|
<Compile Include="Src\DefaultMessageHandler.cs" />
|
||||||
<Compile Include="ILogger.cs" />
|
<Compile Include="Src\Framework\ILogger.cs" />
|
||||||
<Compile Include="IMessageFormatter.cs" />
|
<Compile Include="Src\Framework\ILogHandler.cs" />
|
||||||
<Compile Include="ILogHandler.cs" />
|
<Compile Include="Src\Framework\IMessageFormatter.cs" />
|
||||||
<Compile Include="IMessageHandler.cs" />
|
<Compile Include="Src\Framework\IMessageHandler.cs" />
|
||||||
<Compile Include="LogHandlers\ConsoleLogHandler.cs" />
|
<Compile Include="Src\Framework\LoggerManager.cs" />
|
||||||
<Compile Include="LogHandlers\ConsoleWindowLogHandler.cs" />
|
<Compile Include="Src\Framework\Util.cs" />
|
||||||
<Compile Include="LogHandlers\FileLogHandler.cs" />
|
<Compile Include="Src\LogHandlers\ConsoleLogHandler.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Src\LogHandlers\ConsoleWindowLogHandler.cs" />
|
||||||
<Compile Include="Util.cs" />
|
<Compile Include="Src\LogHandlers\FileLogHandler.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<Import Project="..\Versioning.targets" />
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
|
||||||
<Target Name="BeforeBuild">
|
|
||||||
</Target>
|
|
||||||
<Target Name="AfterBuild">
|
|
||||||
</Target>
|
|
||||||
-->
|
|
||||||
</Project>
|
</Project>
|
@ -1,35 +1,62 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default logger class; illustrates how to use
|
||||||
|
/// the SimpleLog framework.
|
||||||
|
/// </summary>
|
||||||
public class DefaultLogger : ILogger {
|
public class DefaultLogger : ILogger {
|
||||||
|
|
||||||
|
#region Public members
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The singleton instance of this logger. All interactions
|
/// Gets or sets whether this logger is enabled
|
||||||
/// with the logger should be done via this static property.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly DefaultLogger Instance = new DefaultLogger();
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets whether this logger is enabled or not
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Enabled;
|
public bool Enabled;
|
||||||
|
#endregion
|
||||||
|
|
||||||
protected List<ILogHandler> LogHandlers;
|
#region Protected members
|
||||||
|
protected List<ILogHandler> logHandlers;
|
||||||
protected IMessageHandler messageHandler;
|
protected IMessageHandler messageHandler;
|
||||||
protected LogLevel logLevel;
|
protected LogLevel logLevel;
|
||||||
protected string dateFormat;
|
protected string dateFormat;
|
||||||
protected string lineTerminator;
|
protected string lineTerminator;
|
||||||
|
#endregion
|
||||||
|
|
||||||
private DefaultLogger() {
|
#region Constructors
|
||||||
this.LogHandlers = new List<ILogHandler>();
|
/// <summary>
|
||||||
this.logLevel = SimpleLog.LogLevel.Warning;
|
/// Creates a new DefaultLogger with the default settings
|
||||||
this.lineTerminator = SimpleLog.LineTerminator.Unix;
|
/// </summary>
|
||||||
this.Enabled = true;
|
public DefaultLogger() : this(LogLevel.Warning) {
|
||||||
this.messageHandler = new DefaultMessageHandler();
|
|
||||||
this.dateFormat = "yyyy-MM-dd HH:mm:ss";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new DefaultLogger initialized to the specified log level
|
||||||
|
/// </summary>
|
||||||
|
public DefaultLogger(LogLevel logLevel) : this(new DefaultMessageHandler(), "yyyy-MM-dd HH:mm:ss", logLevel, SimpleLog.Framework.LineTerminator.Unix) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new DefaultLogger
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="messageHandler">A message handler</param>
|
||||||
|
/// <param name="dateFormat">A date format suitable as an argument to string.Format()</param>
|
||||||
|
/// <param name="logLevel">The default log level</param>
|
||||||
|
/// <param name="lineTerminator">The line terminator</param>
|
||||||
|
public DefaultLogger(IMessageHandler messageHandler, string dateFormat, LogLevel logLevel, string lineTerminator) {
|
||||||
|
this.LogHandlers = new List<ILogHandler>();
|
||||||
|
this.LogLevel = logLevel;
|
||||||
|
this.LineTerminator = lineTerminator;
|
||||||
|
this.Enabled = true;
|
||||||
|
this.MessageHandler = messageHandler;
|
||||||
|
this.DateFormat = dateFormat;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region ILogger members
|
#region ILogger members
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logs a message to all registered handlers
|
/// Logs a message to all registered handlers
|
||||||
@ -37,14 +64,14 @@ namespace SimpleLog {
|
|||||||
/// <param name="message">The message to log</param>
|
/// <param name="message">The message to log</param>
|
||||||
/// <param name="level">The log level of the message</param>
|
/// <param name="level">The log level of the message</param>
|
||||||
/// <returns>TRUE if all handlers successfully logged the message, FALSE otherwise</returns>
|
/// <returns>TRUE if all handlers successfully logged the message, FALSE otherwise</returns>
|
||||||
public bool Log(object message, LogLevel level) {
|
public virtual bool Log(object message, LogLevel level) {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
if (this.Enabled) {
|
if (this.Enabled) {
|
||||||
string convertedMessage;
|
string convertedMessage;
|
||||||
LogLevel allowedLevel;
|
LogLevel allowedLevel;
|
||||||
IMessageHandler messageHandler;
|
IMessageHandler messageHandler;
|
||||||
foreach (ILogHandler handler in this.LogHandlers) {
|
foreach (ILogHandler handler in this.logHandlers) {
|
||||||
allowedLevel = handler.LogLevel ?? (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;
|
||||||
@ -65,20 +92,19 @@ namespace SimpleLog {
|
|||||||
/// Registers a log handler
|
/// Registers a log handler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void RegisterLogHandler(ILogHandler handler) {
|
public void RegisterLogHandler(ILogHandler handler) {
|
||||||
this.LogHandlers.Add(handler);
|
this.logHandlers.Add(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unregisters all log handlers that are instances or
|
/// Unregisters all log handlers that are instances or
|
||||||
/// derived instances of the specified class
|
/// derived instances of the specified class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="qualifiedClassName">The fully qualified class name (e.g. SimpleLog.ILogHandler</param>
|
/// <param name="qualifiedClassName">The fully qualified class name (e.g. SimpleLog.Framework.ILogHandler)</param>
|
||||||
/// <returns>The number of log handlers that were removed</returns>
|
/// <returns>The number of log handlers that were removed</returns>
|
||||||
public int UnregisterLogHandlerType(string qualifiedClassName) {
|
public int UnregisterLogHandlerType(string qualifiedClassName) {
|
||||||
return this.LogHandlers.RemoveAll(
|
return this.logHandlers.RemoveAll(
|
||||||
delegate(ILogHandler handler) {
|
delegate(ILogHandler handler) {
|
||||||
//Console.WriteLine(handler.GetType().ToString() == qualifiedClassName);
|
return (handler.GetType().FullName == qualifiedClassName);
|
||||||
return (handler.GetType().ToString() == qualifiedClassName);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -89,10 +115,13 @@ namespace SimpleLog {
|
|||||||
/// <returns>The number of log handlers that were removed</returns>
|
/// <returns>The number of log handlers that were removed</returns>
|
||||||
public int UnregisterAllLogHandlers() {
|
public int UnregisterAllLogHandlers() {
|
||||||
int count = this.LogHandlerCount;
|
int count = this.LogHandlerCount;
|
||||||
this.LogHandlers.RemoveRange(0, count);
|
this.logHandlers.RemoveRange(0, count);
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the message handler
|
||||||
|
/// </summary>
|
||||||
public IMessageHandler MessageHandler {
|
public IMessageHandler MessageHandler {
|
||||||
get {
|
get {
|
||||||
return this.messageHandler;
|
return this.messageHandler;
|
||||||
@ -101,6 +130,10 @@ namespace SimpleLog {
|
|||||||
this.messageHandler = value;
|
this.messageHandler = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the log level
|
||||||
|
/// </summary>
|
||||||
public LogLevel LogLevel {
|
public LogLevel LogLevel {
|
||||||
get {
|
get {
|
||||||
return this.logLevel;
|
return this.logLevel;
|
||||||
@ -109,6 +142,10 @@ namespace SimpleLog {
|
|||||||
this.logLevel = value;
|
this.logLevel = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the format used for dates
|
||||||
|
/// </summary>
|
||||||
public string DateFormat {
|
public string DateFormat {
|
||||||
get {
|
get {
|
||||||
return this.dateFormat;
|
return this.dateFormat;
|
||||||
@ -117,6 +154,10 @@ namespace SimpleLog {
|
|||||||
this.dateFormat = value;
|
this.dateFormat = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the line terminator
|
||||||
|
/// </summary>
|
||||||
public string LineTerminator {
|
public string LineTerminator {
|
||||||
get {
|
get {
|
||||||
return this.lineTerminator;
|
return this.lineTerminator;
|
||||||
@ -131,6 +172,19 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Accessors
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the list of registered log handlers
|
||||||
|
/// </summary>
|
||||||
|
protected List<ILogHandler> LogHandlers {
|
||||||
|
get {
|
||||||
|
return this.logHandlers;
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
this.logHandlers = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the number of registered log handlers
|
/// Gets the number of registered log handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -139,7 +193,9 @@ namespace SimpleLog {
|
|||||||
return this.LogHandlers.Count;
|
return this.LogHandlers.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Convenience methods
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logs a debug message
|
/// Logs a debug message
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -174,6 +230,7 @@ namespace SimpleLog {
|
|||||||
public bool Critical(object message) {
|
public bool Critical(object message) {
|
||||||
return this.Log(message, LogLevel.Critical);
|
return this.Log(message, LogLevel.Critical);
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog {
|
namespace SimpleLog {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -9,20 +10,52 @@ namespace SimpleLog {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DefaultMessageHandler : IMessageHandler, IMessageFormatter {
|
public class DefaultMessageHandler : IMessageHandler, IMessageFormatter {
|
||||||
|
|
||||||
|
#region Protected members
|
||||||
protected string dateFormat;
|
protected string dateFormat;
|
||||||
protected string lineTerminator;
|
protected string lineTerminator;
|
||||||
protected string context;
|
protected string context;
|
||||||
protected string delimiter;
|
protected string delimiter;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a DefaultMessageHandler with the default settings
|
||||||
|
/// </summary>
|
||||||
|
public DefaultMessageHandler() : this("") {
|
||||||
|
|
||||||
public DefaultMessageHandler() {
|
|
||||||
this.dateFormat = null;
|
|
||||||
this.lineTerminator = SimpleLog.LineTerminator.Unix;
|
|
||||||
this.context = "";
|
|
||||||
this.delimiter = "\t";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a DefaultMessageHandler with the specified context
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">Context of the message handler</param>
|
||||||
|
public DefaultMessageHandler(string context) : this(context, null, SimpleLog.Framework.LineTerminator.Unix, "\t") {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a DefaultMessageHandler
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">Context of the message handler</param>
|
||||||
|
/// <param name="dateFormat">Date format suitable as an argument to string.Format()</param>
|
||||||
|
/// <param name="lineTerminator">The line terminator</param>
|
||||||
|
/// <param name="delimiter">The string that delimits each part of the log message</param>
|
||||||
|
public DefaultMessageHandler(string context, string dateFormat, string lineTerminator, string delimiter) {
|
||||||
|
this.DateFormat = dateFormat;
|
||||||
|
this.LineTerminator = lineTerminator;
|
||||||
|
this.Context = context;
|
||||||
|
this.Delimiter = delimiter;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region IMessageHandler Members
|
#region IMessageHandler Members
|
||||||
public string FormatLogMessage(string message, LogLevel level) {
|
/// <summary>
|
||||||
|
/// Formats a log message
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">The message to log</param>
|
||||||
|
/// <param name="level">The log level of the message</param>
|
||||||
|
/// <returns>The formatted message</returns>
|
||||||
|
public virtual 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());
|
||||||
@ -42,7 +75,7 @@ namespace SimpleLog {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message object to convert</param>
|
/// <param name="message">The message object to convert</param>
|
||||||
/// <returns>The message as a string</returns>
|
/// <returns>The message as a string</returns>
|
||||||
public string ConvertMessageToString(object message) {
|
public virtual string ConvertMessageToString(object message) {
|
||||||
string msg = null;
|
string msg = null;
|
||||||
if (message is Exception) {
|
if (message is Exception) {
|
||||||
Exception e = (Exception)message;
|
Exception e = (Exception)message;
|
||||||
@ -67,6 +100,7 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Accessors
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the delimiter used to separate log message data
|
/// Gets or sets the delimiter used to separate log message data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -78,8 +112,12 @@ namespace SimpleLog {
|
|||||||
this.delimiter = value;
|
this.delimiter = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region IMessageFormatter members
|
#region IMessageFormatter members
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the date format
|
||||||
|
/// </summary>
|
||||||
public string DateFormat {
|
public string DateFormat {
|
||||||
get {
|
get {
|
||||||
return this.dateFormat;
|
return this.dateFormat;
|
||||||
@ -88,6 +126,10 @@ namespace SimpleLog {
|
|||||||
this.dateFormat = value;
|
this.dateFormat = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the line terminator
|
||||||
|
/// </summary>
|
||||||
public string LineTerminator {
|
public string LineTerminator {
|
||||||
get {
|
get {
|
||||||
return this.lineTerminator;
|
return this.lineTerminator;
|
@ -1,4 +1,4 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog.Framework {
|
||||||
public interface ILogHandler {
|
public interface ILogHandler {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
@ -1,4 +1,4 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog.Framework {
|
||||||
public interface ILogger : IMessageFormatter {
|
public interface ILogger : IMessageFormatter {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
@ -1,4 +1,4 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog.Framework {
|
||||||
public interface IMessageFormatter {
|
public interface IMessageFormatter {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
@ -1,4 +1,4 @@
|
|||||||
namespace SimpleLog {
|
namespace SimpleLog.Framework {
|
||||||
|
|
||||||
public interface IMessageHandler : IMessageFormatter {
|
public interface IMessageHandler : IMessageFormatter {
|
||||||
|
|
51
SimpleLog/Src/Framework/LoggerManager.cs
Normal file
51
SimpleLog/Src/Framework/LoggerManager.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SimpleLog.Framework {
|
||||||
|
/// <summary>
|
||||||
|
/// Manages ILogger instances in a singletonish way and provides
|
||||||
|
/// a single point of entry to creating loggers. If singleton
|
||||||
|
/// behavior is not desired, just instantiate each logger class
|
||||||
|
/// normally.
|
||||||
|
/// </summary>
|
||||||
|
public static class LoggerManager {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registry of ILogger instances
|
||||||
|
/// </summary>
|
||||||
|
private static Dictionary<string, ILogger> registry = new Dictionary<string, ILogger>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a logger from the registry.
|
||||||
|
/// <para>This will reuse a logger that has already been instantiated,
|
||||||
|
/// or create a new instance and stick it in the registry for
|
||||||
|
/// later use.</para>
|
||||||
|
///
|
||||||
|
/// <para>The specified class must implement the SimpleLog.Framework.ILogger
|
||||||
|
/// interface</para>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <code>DefaultLogger logger = LogManager.GetLogger{DefaultLogger}();</code>
|
||||||
|
/// <typeparam name="T">Must implement the SimpleLog.Framework.ILogger interface</typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T GetLogger<T>() {
|
||||||
|
Type type = typeof(T);
|
||||||
|
string index = type.FullName;
|
||||||
|
|
||||||
|
if (LoggerManager.registry.ContainsKey(index)) {
|
||||||
|
ILogger logger;
|
||||||
|
LoggerManager.registry.TryGetValue(index, out logger);
|
||||||
|
return (T)logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Type interfaceType = type.GetInterface("ILogger");
|
||||||
|
if (type.GetInterface("ILogger") != null && type.IsClass) {
|
||||||
|
return (T)Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ArgumentException("The type specified (" + index + ") does not implement the " + (typeof(ILogger).FullName) + " interface");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,37 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimpleLog {
|
namespace SimpleLog.Framework {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Log level enumeration
|
||||||
|
/// </summary>
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum LogLevel {
|
public enum LogLevel {
|
||||||
|
/// <summary>
|
||||||
|
/// Debug message
|
||||||
|
/// </summary>
|
||||||
Debug = 1,
|
Debug = 1,
|
||||||
|
/// <summary>
|
||||||
|
/// Informational message
|
||||||
|
/// </summary>
|
||||||
Info = 2,
|
Info = 2,
|
||||||
Warning = 4,
|
/// <summary>
|
||||||
Error = 8,
|
/// Warning message
|
||||||
Critical = 16
|
/// </summary>
|
||||||
|
Warning = 3,
|
||||||
|
/// <summary>
|
||||||
|
/// Error message
|
||||||
|
/// </summary>
|
||||||
|
Error = 4,
|
||||||
|
/// <summary>
|
||||||
|
/// Critical death message
|
||||||
|
/// </summary>
|
||||||
|
Critical = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Struct representing the different line terminators
|
||||||
|
/// </summary>
|
||||||
public struct LineTerminator {
|
public struct LineTerminator {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Windows line terminator (CRLF)
|
/// Windows line terminator (CRLF)
|
||||||
@ -26,6 +47,9 @@ namespace SimpleLog {
|
|||||||
public static readonly string Unix = "\n";
|
public static readonly string Unix = "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Utility class
|
||||||
|
/// </summary>
|
||||||
public static class Util {
|
public static class Util {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog.LogHandlers {
|
namespace SimpleLog.LogHandlers {
|
||||||
|
|
||||||
@ -86,7 +87,7 @@ namespace SimpleLog.LogHandlers {
|
|||||||
/// <param name="level">The log level</param>
|
/// <param name="level">The log level</param>
|
||||||
/// <returns>One of the STD_* constants</returns>
|
/// <returns>One of the STD_* constants</returns>
|
||||||
protected virtual uint GetStreamType(LogLevel level) {
|
protected virtual uint GetStreamType(LogLevel level) {
|
||||||
if (level >= SimpleLog.LogLevel.Warning) {
|
if (level >= SimpleLog.Framework.LogLevel.Warning) {
|
||||||
return STD_ERROR_HANDLE;
|
return STD_ERROR_HANDLE;
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Diagnostics;
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog.LogHandlers {
|
namespace SimpleLog.LogHandlers {
|
||||||
|
|
||||||
@ -19,8 +19,6 @@ namespace SimpleLog.LogHandlers {
|
|||||||
HighIntensity = 0x0008,
|
HighIntensity = 0x0008,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Log handler for writing log messages to a console window (e.g. cmd.exe).
|
/// Log handler for writing log messages to a console window (e.g. cmd.exe).
|
||||||
///
|
///
|
||||||
@ -76,15 +74,15 @@ namespace SimpleLog.LogHandlers {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual ConsoleColor GetConsoleColors(LogLevel level) {
|
protected virtual ConsoleColor GetConsoleColors(LogLevel level) {
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case SimpleLog.LogLevel.Debug:
|
case SimpleLog.Framework.LogLevel.Debug:
|
||||||
return ConsoleColor.Green;
|
return ConsoleColor.Green;
|
||||||
case SimpleLog.LogLevel.Info:
|
case SimpleLog.Framework.LogLevel.Info:
|
||||||
return ConsoleColor.White;
|
return ConsoleColor.White;
|
||||||
case SimpleLog.LogLevel.Warning:
|
case SimpleLog.Framework.LogLevel.Warning:
|
||||||
return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
|
return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
|
||||||
case SimpleLog.LogLevel.Error:
|
case SimpleLog.Framework.LogLevel.Error:
|
||||||
return ConsoleColor.Red | ConsoleColor.HighIntensity;
|
return ConsoleColor.Red | ConsoleColor.HighIntensity;
|
||||||
case SimpleLog.LogLevel.Critical:
|
case SimpleLog.Framework.LogLevel.Critical:
|
||||||
return (ConsoleColor.White | ConsoleColor.HighIntensity) + ((int)(ConsoleColor.Red | ConsoleColor.HighIntensity) << 4);
|
return (ConsoleColor.White | ConsoleColor.HighIntensity) + ((int)(ConsoleColor.Red | ConsoleColor.HighIntensity) << 4);
|
||||||
default:
|
default:
|
||||||
return ConsoleColor.White;
|
return ConsoleColor.White;
|
@ -1,8 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using SimpleLog.Framework;
|
||||||
|
|
||||||
namespace SimpleLog.LogHandlers {
|
namespace SimpleLog.LogHandlers {
|
||||||
public class FileLogHandler : ILogHandler {
|
public class FileLogHandler : ILogHandler {
|
||||||
@ -71,7 +69,7 @@ namespace SimpleLog.LogHandlers {
|
|||||||
try {
|
try {
|
||||||
File.AppendAllText(fileName, message);
|
File.AppendAllText(fileName, message);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
59
Versioning.targets
Normal file
59
Versioning.targets
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<Producer Condition="$(Producer) == ''">Tommy Montgomery</Producer>
|
||||||
|
<Copyright Condition="$(Copyright) == ''">Copyright (C) 2009 Tommy Montgomery</Copyright>
|
||||||
|
<MajorVersion Condition="$(MajorVersion) == ''">1</MajorVersion>
|
||||||
|
<MinorVersion Condition="$(MinorVersion) == ''">1</MinorVersion>
|
||||||
|
<BuildNumber Condition="$(BuildNumber) == ''">0</BuildNumber>
|
||||||
|
<RevisionNumber Condition="$(RevisionNumber) == ''">0</RevisionNumber>
|
||||||
|
<PropertiesDir>Properties</PropertiesDir>
|
||||||
|
<BuildDependsOn>
|
||||||
|
ConfigureVersionNumber;
|
||||||
|
CreateAssemblyInfo;
|
||||||
|
$(BuildDependsOn)
|
||||||
|
</BuildDependsOn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<AssemblyFile Include="$(PropertiesDir)\AssemblyInfo.cs"/>
|
||||||
|
<Compile Include="@(AssemblyFile)"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<Target Name="GetSubversionInfo">
|
||||||
|
<Message Text="Getting revision information..."/>
|
||||||
|
<SvnInfo RepositoryPath=".">
|
||||||
|
<Output TaskParameter="LastChangedRevision" PropertyName="RevisionNumber"/>
|
||||||
|
</SvnInfo>
|
||||||
|
|
||||||
|
<Message Text="Last changed revision: $(RevisionNumber)"/>
|
||||||
|
</Target>
|
||||||
|
|
||||||
|
<Target Name="ConfigureVersionNumber" DependsOnTargets="GetSubversionInfo">
|
||||||
|
<CreateProperty Value="$(MajorVersion).$(MinorVersion).$(BuildNumber).$(RevisionNumber)">
|
||||||
|
<Output PropertyName="VersionNumber" TaskParameter="Value"/>
|
||||||
|
</CreateProperty>
|
||||||
|
<Message Text="Version number set to: $(VersionNumber)"/>
|
||||||
|
</Target>
|
||||||
|
|
||||||
|
<Target Name="CreateAssemblyInfo" DependsOnTargets="ConfigureVersionNumber">
|
||||||
|
<MakeDir Directories="$(PropertiesDir)"/>
|
||||||
|
<AssemblyInfo
|
||||||
|
OutputFile="@(AssemblyFile)"
|
||||||
|
CodeLanguage="CS"
|
||||||
|
AssemblyCompany="$(Producer)"
|
||||||
|
AssemblyConfiguration="$(Configuration)"
|
||||||
|
AssemblyCopyright="$(Copyright)"
|
||||||
|
AssemblyFileVersion="$(VersionNumber)"
|
||||||
|
AssemblyTitle="$(AssemblyName)"
|
||||||
|
AssemblyVersion="$(VersionNumber)"
|
||||||
|
/>
|
||||||
|
</Target>
|
||||||
|
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
<RemoveDir Directories="$(PropertiesDir)"/>
|
||||||
|
</Target>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user