From 026437d3998efeb3bd2a51f33052467fe34b4924 Mon Sep 17 00:00:00 2001 From: tmont Date: Sun, 12 Apr 2009 07:08:36 +0000 Subject: [PATCH] added file log handler --- SimpleLog/LogHandlers/FileLogHandler.cs | 140 ++++++++++++++++++++++++ SimpleLog/SimpleLog.csproj | 1 + 2 files changed, 141 insertions(+) create mode 100644 SimpleLog/LogHandlers/FileLogHandler.cs diff --git a/SimpleLog/LogHandlers/FileLogHandler.cs b/SimpleLog/LogHandlers/FileLogHandler.cs new file mode 100644 index 0000000..4cdb5b1 --- /dev/null +++ b/SimpleLog/LogHandlers/FileLogHandler.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace SimpleLog.LogHandlers { + public class FileLogHandler : ILogHandler { + + protected IMessageHandler messageHandler; + protected LogLevel? logLevel; + + protected string logDirectory; + protected string filePrefix; + protected string fileSuffix; + + #region Constructors + /// + /// Constructs a new FileLogHandler with the default + /// log directory (temp directory), prefix (log) and + /// suffix (.log) + /// + public FileLogHandler() : this(Path.GetTempPath(), "log", ".log") { + + } + + /// + /// Constructs a new FileLogHandler + /// + /// The directory to log to + /// The file prefix + /// The file suffix (e.g. ".log") + public FileLogHandler(string dir, string prefix, string suffix) { + this.messageHandler = new DefaultMessageHandler(); + this.logLevel = null; + this.logDirectory = dir; + this.filePrefix = prefix; + this.fileSuffix = suffix; + } + #endregion + + #region ILogHandler Members + /// + /// Gracefully shuts down the log handler + /// + public void GracefulShutDown() { + throw new NotImplementedException(); + } + + /// + /// Gets or sets the log level + /// + public LogLevel? LogLevel { + get { + return this.logLevel; + } + set { + this.logLevel = value; + } + } + + /// + /// Logs a message to a file defined by LogDirectory, FilePrefix and + /// FileSuffix (see BuildFileName()) at the specified log level + /// + /// The message to log + /// The log level + public virtual bool Log(string message, LogLevel level) { + string fileName = this.BuildFileName(); + + try { + File.AppendAllText(fileName, message); + } + catch (Exception e) { + return false; + } + + return true; + } + + /// + /// Gets or sets the message handler used by this log handler + /// + public IMessageHandler MessageHandler { + get { + return this.messageHandler; + } + set { + this.messageHandler = value; + } + } + #endregion + + /// + /// Builds the log file name + /// + protected virtual string BuildFileName() { + string fileName = this.logDirectory + Path.DirectorySeparatorChar + this.FilePrefix; + fileName += "_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + this.FileSuffix; + return fileName; + } + + /// + /// Gets or sets the directory to log to + /// + public string LogDirectory { + get { + return this.logDirectory; + } + set { + this.logDirectory = value; + } + } + + /// + /// Gets or sets the filename prefix + /// + public string FilePrefix { + get { + return this.filePrefix; + } + set { + this.filePrefix = value; + } + } + + /// + /// Gets or sets the filename suffix + /// + public string FileSuffix { + get { + return this.fileSuffix; + } + set { + this.fileSuffix = value; + } + } + + } +} diff --git a/SimpleLog/SimpleLog.csproj b/SimpleLog/SimpleLog.csproj index ea458c2..ac0d2c2 100644 --- a/SimpleLog/SimpleLog.csproj +++ b/SimpleLog/SimpleLog.csproj @@ -56,6 +56,7 @@ +