From 9cec382640a000bb44932d6f35cee1a453961b6b Mon Sep 17 00:00:00 2001 From: tmont Date: Sat, 11 Apr 2009 10:00:08 +0000 Subject: [PATCH] added SimpleLog stuff --- SimpleLog.sln | 20 +++++ SimpleLog/ILogHandler.cs | 15 ++++ SimpleLog/Logger.cs | 128 +++++++++++++++++++++++++++ SimpleLog/MessageHandler.cs | 12 +++ SimpleLog/Properties/AssemblyInfo.cs | 36 ++++++++ SimpleLog/SimpleLog.csproj | 61 +++++++++++++ 6 files changed, 272 insertions(+) create mode 100644 SimpleLog.sln create mode 100644 SimpleLog/ILogHandler.cs create mode 100644 SimpleLog/Logger.cs create mode 100644 SimpleLog/MessageHandler.cs create mode 100644 SimpleLog/Properties/AssemblyInfo.cs create mode 100644 SimpleLog/SimpleLog.csproj diff --git a/SimpleLog.sln b/SimpleLog.sln new file mode 100644 index 0000000..4ca6a45 --- /dev/null +++ b/SimpleLog.sln @@ -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 diff --git a/SimpleLog/ILogHandler.cs b/SimpleLog/ILogHandler.cs new file mode 100644 index 0000000..4bc389d --- /dev/null +++ b/SimpleLog/ILogHandler.cs @@ -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; } + + } +} diff --git a/SimpleLog/Logger.cs b/SimpleLog/Logger.cs new file mode 100644 index 0000000..657228f --- /dev/null +++ b/SimpleLog/Logger.cs @@ -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 LogHandlers; + protected LogLevel logLevel; + protected string dateFormat; + protected string lineTerminator; + public bool Enabled; + + private Logger() { + this.LogHandlers = new List(); + this.logLevel = LogLevel.Warning; + this.dateFormat = "YYYY-MM-dd HH:ii:ss"; + this.lineTerminator = LineTerminator.Unix; + this.Enabled = true; + } + + /// + /// Logs a message at the specified log level + /// + /// The message to log + /// The log level of the message + /// TRUE if all handler succesfully logged, or false if any of them failed + 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 + /// + /// Gets or sets the global log level + /// + public LogLevel GlobalLogLevel { + get { + return this.logLevel; + } + set { + this.logLevel = value; + } + } + /// + /// Gets or sets the global date format + /// + public string GlobalDateFormat { + get { + return this.dateFormat; + } + set { + this.dateFormat = value; + } + } + /// + /// Gets or sets the global line terminator + /// + 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 + + } +} diff --git a/SimpleLog/MessageHandler.cs b/SimpleLog/MessageHandler.cs new file mode 100644 index 0000000..37e7abc --- /dev/null +++ b/SimpleLog/MessageHandler.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SimpleLog { + public class MessageHandler { + + + + } +} diff --git a/SimpleLog/Properties/AssemblyInfo.cs b/SimpleLog/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..08d2c0f --- /dev/null +++ b/SimpleLog/Properties/AssemblyInfo.cs @@ -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")] diff --git a/SimpleLog/SimpleLog.csproj b/SimpleLog/SimpleLog.csproj new file mode 100644 index 0000000..d467328 --- /dev/null +++ b/SimpleLog/SimpleLog.csproj @@ -0,0 +1,61 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {20AC889F-0D03-453F-8759-4D1887F3BC0C} + Library + Properties + SimpleLog + SimpleLog + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + + + \ No newline at end of file