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]
|
||||
public void TestLog() {
|
||||
Assert.That(this.handler.Log("yay", LogLevel.Critical));
|
||||
Assert.That(this.handler.Log("yay", LogLevel.Debug));
|
||||
Assert.That(this.handler.Log("yay", Framework.LogLevel.Critical));
|
||||
Assert.That(this.handler.Log("yay", Framework.LogLevel.Debug));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -2,6 +2,7 @@
|
||||
using NUnit.Framework;
|
||||
using NUnit.Mocks;
|
||||
using NUnit.Framework.SyntaxHelpers;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog.Tests {
|
||||
[TestFixture]
|
||||
@ -12,8 +13,7 @@ namespace SimpleLog.Tests {
|
||||
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
this.Logger = DefaultLogger.Instance;
|
||||
this.Logger.Enabled = true;
|
||||
this.Logger = new DefaultLogger();
|
||||
this.Logger.MessageHandler = new DefaultMessageHandler();
|
||||
this.Handler = new DynamicMock(typeof(ILogHandler));
|
||||
|
||||
@ -28,19 +28,21 @@ namespace SimpleLog.Tests {
|
||||
|
||||
[Test]
|
||||
public void TestRegisterAndUnregisterLogHandler() {
|
||||
int oldCount = this.Logger.LogHandlerCount;
|
||||
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
||||
|
||||
//this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
||||
|
||||
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
|
||||
|
||||
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);
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1));
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
|
||||
|
||||
this.Logger.UnregisterAllLogHandlers();
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
||||
|
@ -37,7 +37,7 @@ namespace SimpleLog.Tests {
|
||||
expected += "\\[some kind of context\\] ";
|
||||
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="DefaultLoggerTest.cs" />
|
||||
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||
<Compile Include="LoggerManagerTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</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>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<DebugType>None</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<DefineConstants>
|
||||
</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
@ -48,24 +49,21 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DefaultLogger.cs" />
|
||||
<Compile Include="DefaultMessageHandler.cs" />
|
||||
<Compile Include="ILogger.cs" />
|
||||
<Compile Include="IMessageFormatter.cs" />
|
||||
<Compile Include="ILogHandler.cs" />
|
||||
<Compile Include="IMessageHandler.cs" />
|
||||
<Compile Include="LogHandlers\ConsoleLogHandler.cs" />
|
||||
<Compile Include="LogHandlers\ConsoleWindowLogHandler.cs" />
|
||||
<Compile Include="LogHandlers\FileLogHandler.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Util.cs" />
|
||||
<Compile Include="Src\DefaultLogger.cs" />
|
||||
<Compile Include="Src\DefaultMessageHandler.cs" />
|
||||
<Compile Include="Src\Framework\ILogger.cs" />
|
||||
<Compile Include="Src\Framework\ILogHandler.cs" />
|
||||
<Compile Include="Src\Framework\IMessageFormatter.cs" />
|
||||
<Compile Include="Src\Framework\IMessageHandler.cs" />
|
||||
<Compile Include="Src\Framework\LoggerManager.cs" />
|
||||
<Compile Include="Src\Framework\Util.cs" />
|
||||
<Compile Include="Src\LogHandlers\ConsoleLogHandler.cs" />
|
||||
<Compile Include="Src\LogHandlers\ConsoleWindowLogHandler.cs" />
|
||||
<Compile Include="Src\LogHandlers\FileLogHandler.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
<Import Project="..\Versioning.targets" />
|
||||
</Project>
|
@ -1,35 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog {
|
||||
|
||||
/// <summary>
|
||||
/// Default logger class; illustrates how to use
|
||||
/// the SimpleLog framework.
|
||||
/// </summary>
|
||||
public class DefaultLogger : ILogger {
|
||||
|
||||
#region Public members
|
||||
/// <summary>
|
||||
/// The singleton instance of this logger. All interactions
|
||||
/// 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
|
||||
/// Gets or sets whether this logger is enabled
|
||||
/// </summary>
|
||||
public bool Enabled;
|
||||
#endregion
|
||||
|
||||
protected List<ILogHandler> LogHandlers;
|
||||
#region Protected members
|
||||
protected List<ILogHandler> logHandlers;
|
||||
protected IMessageHandler messageHandler;
|
||||
protected LogLevel logLevel;
|
||||
protected string dateFormat;
|
||||
protected string lineTerminator;
|
||||
#endregion
|
||||
|
||||
private DefaultLogger() {
|
||||
this.LogHandlers = new List<ILogHandler>();
|
||||
this.logLevel = SimpleLog.LogLevel.Warning;
|
||||
this.lineTerminator = SimpleLog.LineTerminator.Unix;
|
||||
this.Enabled = true;
|
||||
this.messageHandler = new DefaultMessageHandler();
|
||||
this.dateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Creates a new DefaultLogger with the default settings
|
||||
/// </summary>
|
||||
public DefaultLogger() : this(LogLevel.Warning) {
|
||||
|
||||
}
|
||||
|
||||
/// <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
|
||||
/// <summary>
|
||||
/// Logs a message to all registered handlers
|
||||
@ -37,14 +64,14 @@ namespace SimpleLog {
|
||||
/// <param name="message">The message to log</param>
|
||||
/// <param name="level">The log level of the message</param>
|
||||
/// <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;
|
||||
|
||||
if (this.Enabled) {
|
||||
string convertedMessage;
|
||||
LogLevel allowedLevel;
|
||||
IMessageHandler messageHandler;
|
||||
foreach (ILogHandler handler in this.LogHandlers) {
|
||||
foreach (ILogHandler handler in this.logHandlers) {
|
||||
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
|
||||
if (level >= allowedLevel) {
|
||||
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
||||
@ -65,20 +92,19 @@ namespace SimpleLog {
|
||||
/// Registers a log handler
|
||||
/// </summary>
|
||||
public void RegisterLogHandler(ILogHandler handler) {
|
||||
this.LogHandlers.Add(handler);
|
||||
this.logHandlers.Add(handler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters all log handlers that are instances or
|
||||
/// derived instances of the specified class
|
||||
/// </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>
|
||||
public int UnregisterLogHandlerType(string qualifiedClassName) {
|
||||
return this.LogHandlers.RemoveAll(
|
||||
return this.logHandlers.RemoveAll(
|
||||
delegate(ILogHandler handler) {
|
||||
//Console.WriteLine(handler.GetType().ToString() == qualifiedClassName);
|
||||
return (handler.GetType().ToString() == qualifiedClassName);
|
||||
return (handler.GetType().FullName == qualifiedClassName);
|
||||
}
|
||||
);
|
||||
}
|
||||
@ -89,10 +115,13 @@ namespace SimpleLog {
|
||||
/// <returns>The number of log handlers that were removed</returns>
|
||||
public int UnregisterAllLogHandlers() {
|
||||
int count = this.LogHandlerCount;
|
||||
this.LogHandlers.RemoveRange(0, count);
|
||||
this.logHandlers.RemoveRange(0, count);
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the message handler
|
||||
/// </summary>
|
||||
public IMessageHandler MessageHandler {
|
||||
get {
|
||||
return this.messageHandler;
|
||||
@ -101,6 +130,10 @@ namespace SimpleLog {
|
||||
this.messageHandler = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log level
|
||||
/// </summary>
|
||||
public LogLevel LogLevel {
|
||||
get {
|
||||
return this.logLevel;
|
||||
@ -109,6 +142,10 @@ namespace SimpleLog {
|
||||
this.logLevel = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the format used for dates
|
||||
/// </summary>
|
||||
public string DateFormat {
|
||||
get {
|
||||
return this.dateFormat;
|
||||
@ -117,6 +154,10 @@ namespace SimpleLog {
|
||||
this.dateFormat = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the line terminator
|
||||
/// </summary>
|
||||
public string LineTerminator {
|
||||
get {
|
||||
return this.lineTerminator;
|
||||
@ -131,6 +172,19 @@ namespace SimpleLog {
|
||||
}
|
||||
#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>
|
||||
/// Gets the number of registered log handlers
|
||||
/// </summary>
|
||||
@ -139,7 +193,9 @@ namespace SimpleLog {
|
||||
return this.LogHandlers.Count;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Convenience methods
|
||||
/// <summary>
|
||||
/// Logs a debug message
|
||||
/// </summary>
|
||||
@ -174,6 +230,7 @@ namespace SimpleLog {
|
||||
public bool Critical(object message) {
|
||||
return this.Log(message, LogLevel.Critical);
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog {
|
||||
/// <summary>
|
||||
@ -9,20 +10,52 @@ namespace SimpleLog {
|
||||
/// </summary>
|
||||
public class DefaultMessageHandler : IMessageHandler, IMessageFormatter {
|
||||
|
||||
#region Protected members
|
||||
protected string dateFormat;
|
||||
protected string lineTerminator;
|
||||
protected string context;
|
||||
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
|
||||
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>();
|
||||
messageData.Add(string.Format("{0:" + this.DateFormat + "}", DateTime.Now));
|
||||
messageData.Add(level.ToString().ToUpper());
|
||||
@ -42,7 +75,7 @@ namespace SimpleLog {
|
||||
/// </summary>
|
||||
/// <param name="message">The message object to convert</param>
|
||||
/// <returns>The message as a string</returns>
|
||||
public string ConvertMessageToString(object message) {
|
||||
public virtual string ConvertMessageToString(object message) {
|
||||
string msg = null;
|
||||
if (message is Exception) {
|
||||
Exception e = (Exception)message;
|
||||
@ -67,6 +100,7 @@ namespace SimpleLog {
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Accessors
|
||||
/// <summary>
|
||||
/// Gets or sets the delimiter used to separate log message data
|
||||
/// </summary>
|
||||
@ -78,8 +112,12 @@ namespace SimpleLog {
|
||||
this.delimiter = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IMessageFormatter members
|
||||
/// <summary>
|
||||
/// Gets or sets the date format
|
||||
/// </summary>
|
||||
public string DateFormat {
|
||||
get {
|
||||
return this.dateFormat;
|
||||
@ -88,6 +126,10 @@ namespace SimpleLog {
|
||||
this.dateFormat = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the line terminator
|
||||
/// </summary>
|
||||
public string LineTerminator {
|
||||
get {
|
||||
return this.lineTerminator;
|
@ -1,4 +1,4 @@
|
||||
namespace SimpleLog {
|
||||
namespace SimpleLog.Framework {
|
||||
public interface ILogHandler {
|
||||
|
||||
/// <summary>
|
@ -1,4 +1,4 @@
|
||||
namespace SimpleLog {
|
||||
namespace SimpleLog.Framework {
|
||||
public interface ILogger : IMessageFormatter {
|
||||
|
||||
/// <summary>
|
@ -1,4 +1,4 @@
|
||||
namespace SimpleLog {
|
||||
namespace SimpleLog.Framework {
|
||||
public interface IMessageFormatter {
|
||||
|
||||
/// <summary>
|
@ -1,4 +1,4 @@
|
||||
namespace SimpleLog {
|
||||
namespace SimpleLog.Framework {
|
||||
|
||||
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;
|
||||
|
||||
namespace SimpleLog {
|
||||
namespace SimpleLog.Framework {
|
||||
|
||||
/// <summary>
|
||||
/// Log level enumeration
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum LogLevel {
|
||||
/// <summary>
|
||||
/// Debug message
|
||||
/// </summary>
|
||||
Debug = 1,
|
||||
/// <summary>
|
||||
/// Informational message
|
||||
/// </summary>
|
||||
Info = 2,
|
||||
Warning = 4,
|
||||
Error = 8,
|
||||
Critical = 16
|
||||
/// <summary>
|
||||
/// Warning message
|
||||
/// </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 {
|
||||
/// <summary>
|
||||
/// Windows line terminator (CRLF)
|
||||
@ -26,6 +47,9 @@ namespace SimpleLog {
|
||||
public static readonly string Unix = "\n";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Utility class
|
||||
/// </summary>
|
||||
public static class Util {
|
||||
|
||||
/// <summary>
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog.LogHandlers {
|
||||
|
||||
@ -86,7 +87,7 @@ namespace SimpleLog.LogHandlers {
|
||||
/// <param name="level">The log level</param>
|
||||
/// <returns>One of the STD_* constants</returns>
|
||||
protected virtual uint GetStreamType(LogLevel level) {
|
||||
if (level >= SimpleLog.LogLevel.Warning) {
|
||||
if (level >= SimpleLog.Framework.LogLevel.Warning) {
|
||||
return STD_ERROR_HANDLE;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Diagnostics;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog.LogHandlers {
|
||||
|
||||
@ -19,8 +19,6 @@ namespace SimpleLog.LogHandlers {
|
||||
HighIntensity = 0x0008,
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Log handler for writing log messages to a console window (e.g. cmd.exe).
|
||||
///
|
||||
@ -76,15 +74,15 @@ namespace SimpleLog.LogHandlers {
|
||||
/// </summary>
|
||||
protected virtual ConsoleColor GetConsoleColors(LogLevel level) {
|
||||
switch (level) {
|
||||
case SimpleLog.LogLevel.Debug:
|
||||
case SimpleLog.Framework.LogLevel.Debug:
|
||||
return ConsoleColor.Green;
|
||||
case SimpleLog.LogLevel.Info:
|
||||
case SimpleLog.Framework.LogLevel.Info:
|
||||
return ConsoleColor.White;
|
||||
case SimpleLog.LogLevel.Warning:
|
||||
case SimpleLog.Framework.LogLevel.Warning:
|
||||
return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
|
||||
case SimpleLog.LogLevel.Error:
|
||||
case SimpleLog.Framework.LogLevel.Error:
|
||||
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);
|
||||
default:
|
||||
return ConsoleColor.White;
|
@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using SimpleLog.Framework;
|
||||
|
||||
namespace SimpleLog.LogHandlers {
|
||||
public class FileLogHandler : ILogHandler {
|
||||
@ -71,7 +69,7 @@ namespace SimpleLog.LogHandlers {
|
||||
try {
|
||||
File.AppendAllText(fileName, message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (Exception) {
|
||||
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