/*
* Logger.cs - Simple logging functionality for Level9.Net.
*
* Copyright (C) 2004 - 2011 Andreas Scherrer
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
using System;
using System.IO;
namespace Level9
{
///
/// Simple logging functionality for Level9.Net
///
public class Logger
{
// -- Constants ------------------------------------------------------
///
/// The format of logging timestamps.
///
const string TS_FORMAT = "yyyy-MM-dd HH:mm:ss";
// -- Attributes -----------------------------------------------------
///
/// A stream writer for writing into a logfile.
///
private static StreamWriter sw = null;
// -- Methods --------------------------------------------------------
///
/// Appends a log message to the application logfile.
///
///
/// the object type that raises the log message
///
/// the log message
/// an Exception, or null if none occurred
public static void Log(Type type, string message, Exception e)
{
if (!Config.LoggingEnabled) {
return;
}
try {
if (sw == null) {
sw = File.AppendText(Config.GetLogFilePath());
}
lock(sw) {
sw.WriteLine(DateTime.Now.ToString(TS_FORMAT)
+ " [" + type + "] " + message);
if (e != null) {
sw.WriteLine(e.Message);
sw.WriteLine(e.StackTrace);
}
sw.Flush();
}
}
catch {
// Do nothing
}
}
///
/// Appends a log message to the application logfile.
///
/// the object that raises the log message
/// the log message
/// an Exception, or null if none occurred
public static void Log(Object obj, string message, Exception e)
{
if (obj != null) {
Log(obj.GetType(), message, e);
}
}
///
/// Appends a log message to the application logfile.
///
///
/// the object type that raises the log message
///
/// the log message
public static void Log(Type type, string message)
{
Log(type, message, null);
}
///
/// Appends a log message to the application logfile.
///
/// the object that raises the log message
/// the log message
public static void Log(Object obj, string message)
{
Log(obj, message, null);
}
}
}