using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
namespace MudEngine.Core
{
///
/// Public Engine Logging Class.
///
public static class Logger
{
///
/// The Log Filename for the engine log.
///
public static string LogFilename { get; set; }
///
/// Gets or Sets if the Logger is enabled or disabled.
///
public static Boolean Enabled { get; set; }
///
/// Gets or Sets if the logger will output it's information to the Console.
///
public static Boolean ConsoleOutPut { get; set; }
///
/// Clears the queued log messages from cache
///
public static void ClearLog()
{
//If the log file exists, delete it.
if (String.IsNullOrEmpty(LogFilename))
LogFilename = "Engine.Log";
if (System.IO.File.Exists(LogFilename))
System.IO.File.Delete(LogFilename);
//Clear the cache.
_Messages.Clear();
}
///
/// Writes a single line to the engine log file.
///
///
public static void WriteLine(String message)
{
//Only write to log if enabled.
if (!Enabled)
return;
//Make sure we have a valid filename
if (String.IsNullOrEmpty(LogFilename))
LogFilename = "Engine.Log";
//Ensure that the log messages cache is not null
if (_Messages == null)
_Messages = new List();
//Get the current time and format it
String Time = DateTime.Now.ToString("h:mm:ss:ff tt");
//Output to console if enabled.
if (ConsoleOutPut)
Console.WriteLine(Time + ": " + message);
//Try to write the message to the log file.
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(LogFilename, true))
{
//Write the message to file
file.WriteLine(Time + ": " + message);
//Add it to the messages cache.
_Messages.Add(Time + ": " + message);
}
}
catch
{
throw new Exception("Unable to write message (" + message + ") to log file (" + LogFilename + ").");
}
}
///
/// Returns an array of messages that have been queued in the log cache.
///
///
public static String[] GetMessages()
{
if (_Messages == null)
return new string[0];
else
return _Messages.ToArray();
}
private static List _Messages;
}
}