SimpleLog 1.2 branch reintegration

This commit is contained in:
tmont 2009-04-18 04:26:27 +00:00
parent f44b12cf56
commit 7e28eeed30
15 changed files with 182 additions and 125 deletions

View File

@ -29,7 +29,6 @@ namespace SimpleLog.Tests {
[Test]
public void TestRegisterAndUnregisterLogHandler() {
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
//this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;

View File

@ -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.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Bridgepoint Education")]
[assembly: AssemblyProduct("SimpleLog.Tests")]
[assembly: AssemblyCopyright("Copyright © Bridgepoint Education 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("36a0db9c-388f-41a7-a278-0aafa72986bb")]
// 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")]

View File

@ -23,10 +23,10 @@
<WarningLevel>4</WarningLevel>
</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>
@ -34,25 +34,12 @@
<Reference Include="nunit.core, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="nunit.mocks, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConsoleLogHandlerTest.cs" />
<Compile Include="DefaultLoggerTest.cs" />
<Compile Include="DefaultMessageHandlerTest.cs" />
<Compile Include="LoggerManagerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SimpleLog\SimpleLog.csproj">
@ -60,12 +47,9 @@
<Name>SimpleLog</Name>
</ProjectReference>
</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="..\SimpleLog.targets" />
</Project>

View File

@ -3,12 +3,12 @@
<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>
<Producer>Tommy Montgomery</Producer>
<Copyright>Copyright (C) 2009 Tommy Montgomery</Copyright>
<MajorVersion>1</MajorVersion>
<MinorVersion>2</MinorVersion>
<BuildNumber>0</BuildNumber>
<RevisionNumber>0</RevisionNumber>
<PropertiesDir>Properties</PropertiesDir>
<BuildDependsOn>
ConfigureVersionNumber;

View File

@ -12,18 +12,19 @@
<AssemblyName>SimpleLog</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>
</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>
</DocumentationFile>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>None</DebugType>
@ -33,21 +34,8 @@
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\SimpleLog.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Src\DefaultLogger.cs" />
<Compile Include="Src\DefaultMessageHandler.cs" />
@ -65,5 +53,5 @@
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\Versioning.targets" />
<Import Project="..\SimpleLog.targets" />
</Project>

View File

@ -5,8 +5,7 @@ using SimpleLog.Framework;
namespace SimpleLog {
/// <summary>
/// Default logger class; illustrates how to use
/// the SimpleLog framework.
/// Default, barebones logger. Gets the job done.
/// </summary>
public class DefaultLogger : ILogger {
@ -18,10 +17,25 @@ namespace SimpleLog {
#endregion
#region Protected members
/// <summary>
/// Collection of all registered log handlers
/// </summary>
protected List<ILogHandler> logHandlers;
/// <summary>
/// Message handler used by the logger
/// </summary>
protected IMessageHandler messageHandler;
/// <summary>
/// Global log level
/// </summary>
protected LogLevel logLevel;
/// <summary>
/// Global date format
/// </summary>
protected string dateFormat;
/// <summary>
/// Global line terminator
/// </summary>
protected string lineTerminator;
#endregion
@ -29,7 +43,7 @@ namespace SimpleLog {
/// <summary>
/// Creates a new DefaultLogger with the default settings
/// </summary>
public DefaultLogger() : this(LogLevel.Warning) {
public DefaultLogger() : this(LogLevel.Default) {
}
@ -72,8 +86,8 @@ namespace SimpleLog {
LogLevel allowedLevel;
IMessageHandler messageHandler;
foreach (ILogHandler handler in this.logHandlers) {
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
if (level >= allowedLevel) {
allowedLevel = (handler.LogLevel == SimpleLog.Framework.LogLevel.None) ? (LogLevel)this.LogLevel : handler.LogLevel;
if ((level & allowedLevel) > 0) {
messageHandler = handler.MessageHandler ?? this.MessageHandler;
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;

View File

@ -4,16 +4,28 @@ using SimpleLog.Framework;
namespace SimpleLog {
/// <summary>
/// Default message handler. This class can be extended if slight
/// modification are needed, or it can serve as an example of how
/// to create a message handler.
/// Default message handler
/// </summary>
public class DefaultMessageHandler : IMessageHandler, IMessageFormatter {
#region Protected members
/// <summary>
/// Date format of this message handler
/// </summary>
protected string dateFormat;
/// <summary>
/// Line terminator used by this message handler
/// </summary>
protected string lineTerminator;
/// <summary>
/// Context string used by this message handler
/// </summary>
protected string context;
/// <summary>
/// Message component delimiter (e.g. in the message
/// "2009-04-17 [context] This is the message" a space
/// is the delimiter)
/// </summary>
protected string delimiter;
#endregion

View File

@ -1,4 +1,10 @@
namespace SimpleLog.Framework {
/// <summary>
/// Interface for log handlers. Log handlers are injected
/// into ILoggers. They handle the physical act of actually
/// logging the message to the wherever that message should
/// go (e.g. file, console, output stream, nowhere, etc.).
/// </summary>
public interface ILogHandler {
/// <summary>
@ -9,14 +15,14 @@
/// <summary>
/// Gets or sets the log level
/// </summary>
LogLevel? LogLevel { get; set; }
LogLevel LogLevel { get; set; }
/// <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>
/// <returns>TRUE if all handlers succesfully logged, or false if any of them failed</returns>
bool Log(string message, LogLevel level);
/// <summary>

View File

@ -1,4 +1,12 @@
namespace SimpleLog.Framework {
/// <summary>
/// ILoggers use ILogHandlers and IMessageHandlers
/// to log messages. They delegate the actual work
/// of logging to log handlers and message formatting
/// to the message handler. ILoggers are resonsible
/// for determining when a message should be logged (i.e.
/// handling the log level).
/// </summary>
public interface ILogger : IMessageFormatter {
/// <summary>
@ -7,12 +15,6 @@
/// <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>

View File

@ -1,4 +1,8 @@
namespace SimpleLog.Framework {
/// <summary>
/// Interface for formatting log messages into
/// whatever format they should be in.
/// </summary>
public interface IMessageFormatter {
/// <summary>

View File

@ -1,5 +1,10 @@
namespace SimpleLog.Framework {
/// <summary>
/// This interface handles message objects by converting
/// them to strings and formatting them (i.e. prepending
/// them with a timestamp).
/// </summary>
public interface IMessageHandler : IMessageFormatter {
/// <summary>

View File

@ -8,25 +8,45 @@ namespace SimpleLog.Framework {
[Flags]
public enum LogLevel {
/// <summary>
/// Debug message
/// No log level specified
/// </summary>
None = 0,
/// <summary>
/// Debug level only
/// </summary>
Debug = 1,
/// <summary>
/// Informational message
/// Informational level only
/// </summary>
Info = 2,
/// <summary>
/// Warning message
/// Warning level only
/// </summary>
Warning = 3,
Warning = 4,
/// <summary>
/// Error message
/// Error level only
/// </summary>
Error = 4,
Error = 8,
/// <summary>
/// Critical death message
/// Critical death level only
/// </summary>
Critical = 5
Critical = 16,
/// <summary>
/// Preferred alias of Critical
/// </summary>
OhNoes = 16,
/// <summary>
/// Debug and Info only
/// </summary>
LowPriority = Debug | Info,
/// <summary>
/// Default log level: Warning, Error and Critical
/// </summary>
Default = Warning | Error | Critical,
/// <summary>
/// All messages are logged
/// </summary>
All = Debug | Info | Warning | Error | Critical
}
/// <summary>

View File

@ -8,23 +8,38 @@ namespace SimpleLog.LogHandlers {
/// </summary>
public class ConsoleLogHandler : ILogHandler {
/// <summary>
/// Message handler used by this log handler
/// </summary>
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
/// <summary>
/// Log level of this log handler
/// </summary>
protected LogLevel logLevel;
//stolen from log4net
public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
/// <summary>
/// stdout handle
/// </summary>
public const uint STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
/// <summary>
/// stderr handle
/// </summary>
public const uint STD_ERROR_HANDLE = unchecked((UInt32)(-12));
/// <summary>
/// Constructs a new ConsoleLogHandler with a default
/// message handler
/// </summary>
public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
this.MessageHandler = new DefaultMessageHandler();
this.LogLevel = SimpleLog.Framework.LogLevel.None;
}
#region ILogHandler Members
/// <summary>
/// Gracefully shuts down this log handler
/// </summary>
public virtual void GracefulShutDown() {
//does nothing
}
@ -32,7 +47,7 @@ namespace SimpleLog.LogHandlers {
/// <summary>
/// Gets or sets the log level for this handler
/// </summary>
public LogLevel? LogLevel {
public LogLevel LogLevel {
get {
return this.logLevel;
}

View File

@ -9,13 +9,37 @@ namespace SimpleLog.LogHandlers {
/// </summary>
[Flags]
public enum ConsoleColor : int {
/// <summary>
/// The color blue
/// </summary>
Blue = 0x0001,
/// <summary>
/// The color green
/// </summary>
Green = 0x0002,
/// <summary>
/// The color red
/// </summary>
Red = 0x0004,
/// <summary>
/// The color white (Red, Green and Blue combined)
/// </summary>
White = Blue | Green | Red,
/// <summary>
/// The color yellow (Red and Green combined)
/// </summary>
Yellow = Red | Green,
/// <summary>
/// The color purple (Red and Blue combined)
/// </summary>
Purple = Red | Blue,
/// <summary>
/// The color cyan (Green and Blue combined)
/// </summary>
Cyan = Green | Blue,
/// <summary>
/// Brightens a color
/// </summary>
HighIntensity = 0x0008,
}

View File

@ -3,13 +3,33 @@ using System.IO;
using SimpleLog.Framework;
namespace SimpleLog.LogHandlers {
/// <summary>
/// Log handler for filesystem-based logging
/// </summary>
public class FileLogHandler : ILogHandler {
/// <summary>
/// Message handler used by this log handler
/// </summary>
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
/// <summary>
/// Log level of this log handler
/// </summary>
protected LogLevel logLevel;
/// <summary>
/// Directory in which to create the log files
/// </summary>
protected string logDirectory;
/// <summary>
/// The string that prepends each log file name (e.g.
/// in logger_20090417.log, "logger" is the filePrefix)
/// </summary>
protected string filePrefix;
/// <summary>
/// The file extension of each log file (e.g.
/// in logger_20090417.log, ".log" is the fileSuffix)
/// </summary>
protected string fileSuffix;
#region Constructors
@ -30,10 +50,10 @@ namespace SimpleLog.LogHandlers {
/// <param name="suffix">The file suffix (e.g. ".log")</param>
public FileLogHandler(string dir, string prefix, string suffix) {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
this.logDirectory = dir;
this.filePrefix = prefix;
this.fileSuffix = suffix;
this.LogLevel = SimpleLog.Framework.LogLevel.None;
this.LogDirectory = dir;
this.FilePrefix = prefix;
this.FileSuffix = suffix;
}
#endregion
@ -48,7 +68,7 @@ namespace SimpleLog.LogHandlers {
/// <summary>
/// Gets or sets the log level
/// </summary>
public LogLevel? LogLevel {
public LogLevel LogLevel {
get {
return this.logLevel;
}