added SimpleLog stuff

This commit is contained in:
tmont 2009-04-11 10:00:08 +00:00
commit 9cec382640
6 changed files with 272 additions and 0 deletions

20
SimpleLog.sln Normal file
View File

@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual C# Express 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLog", "SimpleLog\SimpleLog.csproj", "{20AC889F-0D03-453F-8759-4D1887F3BC0C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20AC889F-0D03-453F-8759-4D1887F3BC0C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

15
SimpleLog/ILogHandler.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleLog {
public interface ILogHandler {
bool Log(object message, LogLevel level);
string Context { get; set; }
LogLevel? LogLevel { get; set; }
string DateFormat { get; set; }
}
}

128
SimpleLog/Logger.cs Normal file
View File

@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleLog {
[Flags]
public enum LogLevel {
Debug = 1,
Info = 2,
Warning = 4,
Error = 8,
Critical = 16
}
public struct LineTerminator {
public static readonly string Windows = "\r\n";
public static readonly string Mac = "\r";
public static readonly string Unix = "\n";
}
public class Logger {
public static readonly Logger Instance = new Logger();
protected List<ILogHandler> LogHandlers;
protected LogLevel logLevel;
protected string dateFormat;
protected string lineTerminator;
public bool Enabled;
private Logger() {
this.LogHandlers = new List<ILogHandler>();
this.logLevel = LogLevel.Warning;
this.dateFormat = "YYYY-MM-dd HH:ii:ss";
this.lineTerminator = LineTerminator.Unix;
this.Enabled = true;
}
/// <summary>
/// Logs a message at the specified log level
/// </summary>
/// <param name="message">The message to log</param>
/// <param name="level">The log level of the message</param>
/// <returns>TRUE if all handler succesfully logged, or false if any of them failed</returns>
public bool Log(object message, LogLevel level) {
bool success = true;
if (this.Enabled) {
string convertedMessage = this.ConvertMessageToString(message);
foreach (ILogHandler handler in this.LogHandlers) {
LogLevel allowedLevel = handler.LogLevel ?? this.GlobalLogLevel;
if (level <= allowedLevel) {
convertedMessage = this.ConstructLogMessage(handler, convertedMessage);
success = (success && handler.Log(convertedMessage, allowedLevel));
}
}
}
return success;
}
protected string ConvertMessageToString(object message) {
string msg = null;
if (message is Exception) {
Exception e = (Exception)message;
msg = e.Message + "\n" + e.StackTrace;
}
else {
msg = message.ToString();
}
return msg;
}
protected virtual string ConstructLogMessage(ILogHandler handler, string message) {
string dateFormat = handler.DateFormat ?? this.GlobalDateFormat;
string timestamp = string.Format("{0:" + dateFormat + "}", DateTime.Now);
return timestamp;
}
public void RegisterLogHandler(ILogHandler handler) {
this.LogHandlers.Add(handler);
}
#region Accessors
/// <summary>
/// Gets or sets the global log level
/// </summary>
public LogLevel GlobalLogLevel {
get {
return this.logLevel;
}
set {
this.logLevel = value;
}
}
/// <summary>
/// Gets or sets the global date format
/// </summary>
public string GlobalDateFormat {
get {
return this.dateFormat;
}
set {
this.dateFormat = value;
}
}
/// <summary>
/// Gets or sets the global line terminator
/// </summary>
public string GlobalLineTerminator {
get {
return this.lineTerminator;
}
set {
if (value == LineTerminator.Unix || value == LineTerminator.Windows || value == LineTerminator.Mac) {
this.lineTerminator = value;
}
else {
throw new ArgumentException("Invalid line terminator; see SimpleLog.LineTerminator struct for valid line terminators");
}
}
}
#endregion
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleLog {
public class MessageHandler {
}
}

View File

@ -0,0 +1,36 @@
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("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Bridgepoint Education")]
[assembly: AssemblyProduct("SimpleLog")]
[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("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")]

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{20AC889F-0D03-453F-8759-4D1887F3BC0C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SimpleLog</RootNamespace>
<AssemblyName>SimpleLog</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</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="ILogHandler.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MessageHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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>
-->
</Project>