Been looking for a good logging framework? Check out log4net. I really like the options it gives you, especially for runtime configuration (via a config file or whatever) is very convienient.
Just cause I like log4net so much, I decided to build a quick class that closely emulates the types in the log4net namespace. Here's a quick and easy way to get started using log4net now, without having to invest a lot of time learning log4net "right now." Cause if you're like me, I'm always looking for a quick solution to an easy (but sometimes deep) problem.
namespace log4net
{
namespace Core
{
///
/// Log4Net abstraction, we cheat and build a fake log4net.Core.Level,
/// switch to full blown log4net and simple recompile
///
public enum Level
{
ALL=6,
DEBUG=5,
INFO=4,
WARN=3,
ERROR=2,
FATAL=1,
OFF=0
}
}
///
/// Log4Net abstraction, we cheat and build a fake log4net.LogManager,
/// switch to full blown log4net and simple recompile
///
public class LogManager
{
public static ILog GetLogger(Type type)
{
return new ILog(type);
}
public static ILog GetLogger(string name)
{
return new ILog(name);
}
}
///
/// Log4Net abstraction, we cheat and turn the log4net.ILog interface into an actual implementation,
/// switch to full blown log4net and simple recompile
///
public class ILog
{
public ILog(string name)
{
if (name == null) throw new ArgumentNullException("name");
if (name.Length == 0) throw new ArgumentException("name is empty.");
this._pre = name;
}
public ILog(Type type)
{
if (type == null) throw new ArgumentNullException("type");
this._pre = type.ToString();
}
private string _pre;
//i think all of them should output, if it gets this far. dev's should be checking IsDebug...etc
public void DebugFormat(string format, params object[] args) { WriteFormat(_pre, format, args); }
public void InfoFormat(string format, params object[] args) { WriteFormat(_pre, format, args); }
public void WarnFormat(string format, params object[] args) { WriteFormat(_pre, format, args); }
public void ErrorFormat(string format, params object[] args) { WriteFormat(_pre, format, args); }
public void FatalFormat(string format, params object[] args) { WriteFormat(_pre, format, args); }
#region Write/WriteFormat
private static void WriteFormat(string preamble, string format, params object[] args)
{
Write(preamble, string.Format(format, args));
}
private static void Write(string preamble, string message)
{
try { Trace.WriteLine(preamble + " " + message); }
catch { }
}
#endregion
public void Warn(string message) { Write(_pre, message); }
public void Error(string message) { Write(_pre, message); }
public void Fatal(string message) { Write(_pre, message); }
public void Debug(string message) { Write(_pre, message); }
public void Info(string message) { Write(_pre, message); }
//i'm gonna default Info and Warn.
private bool _debug, _info=true, _warn=true, _error, _fatal;
public bool IsDebugEnabled { get { return _debug; } }
public bool IsInfoEnabled { get { return _info; } }
public bool IsWarnEnabled { get { return _warn; } }
public bool IsErrorEnabled { get { return _error; } }
public bool IsFatalEnabled { get { return _fatal; } }
//private Core.Level _level = Core.Level.DEBUG;
public void SetLevels(bool debug, bool info, bool warn, bool error, bool fatal)
{
this._debug = debug;
this._info = info;
this._warn = warn;
this._error = error;
this._fatal = fatal;
}
public void SetAllLevels(bool boolean)
{
this._debug = this._info = this._warn = this._error = this._fatal = boolean;
}
public void SetOff()
{
SetAllLevels(false);
}
public void Warn(string message, Exception t) { WriteExceptionMessage(_pre, message, t); }
public void Error(string message, Exception t) { WriteExceptionMessage(_pre, message, t); }
public void Fatal(string message, Exception t) { WriteExceptionMessage(_pre, message, t); }
#region WriteException
private static void WriteExceptionMessage(string preamble, string message, Exception t)
{
try
{
Trace.WriteLine(preamble + " " + message + " Exception details:");
try
{
Trace.Indent();
Trace.WriteLine(t.ToString());
}
finally { Trace.Unindent(); }
}
catch { }
}
#endregion
}
}
Then to quickly use it add this private static initializer into the classes that generate log output:
private
static log4net.ILog Log = log4net.LogManager.GetLogger(typeof(QuoteParserBase));
Then simply call Log.Info, Log.Debug, Log.Warn and whatever to get it going:
Log.Debug(”This is a log message during Debug”);
Log.Warn(”This is a warning (maybe during a catch?)”);
Log.Info(”Starting to poll database...”);