added file log handler
This commit is contained in:
parent
c3969fc7f8
commit
026437d399
140
SimpleLog/LogHandlers/FileLogHandler.cs
Normal file
140
SimpleLog/LogHandlers/FileLogHandler.cs
Normal file
@ -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
|
||||||
|
/// <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 e) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -56,6 +56,7 @@
|
|||||||
<Compile Include="IMessageHandler.cs" />
|
<Compile Include="IMessageHandler.cs" />
|
||||||
<Compile Include="LogHandlers\ConsoleLogHandler.cs" />
|
<Compile Include="LogHandlers\ConsoleLogHandler.cs" />
|
||||||
<Compile Include="LogHandlers\ConsoleWindowLogHandler.cs" />
|
<Compile Include="LogHandlers\ConsoleWindowLogHandler.cs" />
|
||||||
|
<Compile Include="LogHandlers\FileLogHandler.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Util.cs" />
|
<Compile Include="Util.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user