// Logger.cs // // Copyright (C) 2002-2004 Ryan Seghers // // This software is provided AS IS. No warranty is granted, // neither expressed nor implied. USE THIS SOFTWARE AT YOUR OWN RISK. // NO REPRESENTATION OF MERCHANTABILITY or FITNESS FOR ANY // PURPOSE is given. // // License to use this software is limited by the following terms: // 1) This code may be used in any program, including programs developed // for commercial purposes, provided that this notice is included verbatim. // // Also, in return for using this code, please attempt to make your fixes and // updates available in some way, such as by sending your updates to the // author. using System; namespace RTools_NTS.Util { /// /// This is a simple implementation of a Logger class. /// The purpose is to threshold output based on a verbosity setting, /// format messages similarly, and collect all message writes such that /// they can be redirected easily. You (and I) should probably use /// the some other library for this, but I haven't found one that suits /// my needs. /// /// /// This doesn't implement much of the functionality possible /// with this interface. This could redirect messages to other /// text writers, forward messages to subscribers, etc. /// public class Logger { // NOTE: modified for "safe" assembly in Sql 2005 // Readonly added! ///Backer for Log. protected static readonly Logger log = new Logger(String.Empty); ///The name is prepended to all messages. protected string name; /// /// The verbosity of this logger. Messages are filtered /// based on this setting. /// protected VerbosityLevel verbosity; /// /// Default constructor. /// public Logger() { name = String.Empty; } /// /// Construct and set name. /// /// The name for this logger. This name /// is prepended to output messages (except Out messages). public Logger(string name) { this.name = name; verbosity = VerbosityLevel.Debug; } /// ///A static instance you can use without creating your own. /// public static Logger Log { get { return (log); } } /// /// The verbosity of this logger. Messages are filtered /// based on this setting. /// public VerbosityLevel Verbosity { get { return (verbosity); } set { verbosity = value; } } /// /// Write a string with no verbosity checking and no formatting. /// /// The format string. /// The arguments. public void Write(string msg, params object[] args) {} /// /// Write a line with no verbosity checking and no formatting. /// /// The format string. /// The arguments. public void WriteLine(string msg, params object[] args) {} /// /// Write a string of this particular verbosity. /// This will not output the string unless the verbosity is /// greater than or equal to this object's threshold. /// This prepends the level of the message and /// the name of this Logger. /// /// The format string. /// The arguments. public void Debug(string msg, params object[] args) { if (verbosity >= VerbosityLevel.Debug) {} } /// /// Write a string of this particular verbosity. /// This will not output the string unless the verbosity is /// greater than or equal to this object's threshold. /// This prepends the level of the message and /// the name of this Logger. /// /// The format string. /// The arguments. public void Info(string msg, params object[] args) { if (verbosity >= VerbosityLevel.Info) {} } /// /// Write a string of this particular verbosity. /// This will not output the string unless the verbosity is /// greater than or equal to this object's threshold. /// This prepends the level of the message and /// the name of this Logger. /// /// The format string. /// The arguments. public void Warn(string msg, params object[] args) { if (verbosity >= VerbosityLevel.Warn) {} } /// /// Write a string of this particular verbosity. /// This will not output the string unless the verbosity is /// greater than or equal to this object's threshold. /// This prepends the level of the message and /// the name of this Logger. /// /// The format string. /// The arguments. public void Error(string msg, params object[] args) { if (verbosity >= VerbosityLevel.Error) {} } /// /// A simple static self test method. /// /// bool - currently always true public static bool TestSelf() { Logger log = new Logger("Logger: TestSelf"); log.Debug("A debug message."); log.Info("An info message."); log.Warn("A warn message."); log.Error("An error message."); log.Write("Some writes: "); for (int i = 0; i < 5; i++) { log.Write("{0},", i); } log.WriteLine(String.Empty); log.WriteLine("This WriteLine message will always be displayed."); return (true); } } }