svn merge http://svn.tommymontgomery.com/SimpleLog/trunk http://svn.tommymontgomery.com/SimpleLog/branches/1.1
139 lines
3.2 KiB
C#
139 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using SimpleLog.Framework;
|
|
|
|
namespace SimpleLog.LogHandlers {
|
|
public class FileLogHandler : ILogHandler {
|
|
|
|
protected IMessageHandler messageHandler;
|
|
protected LogLevel? logLevel;
|
|
|
|
protected string logDirectory;
|
|
protected string filePrefix;
|
|
protected string fileSuffix;
|
|
|
|
#region Constructors
|
|
/// <summary>
|
|
/// Constructs a new FileLogHandler with the default
|
|
/// log directory (temp directory), prefix (log) and
|
|
/// suffix (.log)
|
|
/// </summary>
|
|
public FileLogHandler() : this(Path.GetTempPath(), "log", ".log") {
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a new FileLogHandler
|
|
/// </summary>
|
|
/// <param name="dir">The directory to log to</param>
|
|
/// <param name="prefix">The file prefix</param>
|
|
/// <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;
|
|
}
|
|
#endregion
|
|
|
|
#region ILogHandler Members
|
|
/// <summary>
|
|
/// Gracefully shuts down the log handler
|
|
/// </summary>
|
|
public void GracefulShutDown() {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the log level
|
|
/// </summary>
|
|
public LogLevel? LogLevel {
|
|
get {
|
|
return this.logLevel;
|
|
}
|
|
set {
|
|
this.logLevel = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs a message to a file defined by LogDirectory, FilePrefix and
|
|
/// FileSuffix (see BuildFileName()) at the specified log level
|
|
/// </summary>
|
|
/// <param name="message">The message to log</param>
|
|
/// <param name="level">The log level</param>
|
|
public virtual bool Log(string message, LogLevel level) {
|
|
string fileName = this.BuildFileName();
|
|
|
|
try {
|
|
File.AppendAllText(fileName, message);
|
|
}
|
|
catch (Exception) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the message handler used by this log handler
|
|
/// </summary>
|
|
public IMessageHandler MessageHandler {
|
|
get {
|
|
return this.messageHandler;
|
|
}
|
|
set {
|
|
this.messageHandler = value;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Builds the log file name
|
|
/// </summary>
|
|
protected virtual string BuildFileName() {
|
|
string fileName = this.logDirectory + Path.DirectorySeparatorChar + this.FilePrefix;
|
|
fileName += "_" + string.Format("{0:yyyyMMdd}", DateTime.Now) + this.FileSuffix;
|
|
return fileName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the directory to log to
|
|
/// </summary>
|
|
public string LogDirectory {
|
|
get {
|
|
return this.logDirectory;
|
|
}
|
|
set {
|
|
this.logDirectory = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the filename prefix
|
|
/// </summary>
|
|
public string FilePrefix {
|
|
get {
|
|
return this.filePrefix;
|
|
}
|
|
set {
|
|
this.filePrefix = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the filename suffix
|
|
/// </summary>
|
|
public string FileSuffix {
|
|
get {
|
|
return this.fileSuffix;
|
|
}
|
|
set {
|
|
this.fileSuffix = value;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|