//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOAuth { using System; using System.Globalization; using DotNetOAuth.Loggers; using log4net.Core; /// /// A general logger for the entire DotNetOAuth library. /// /// /// Because this logger is intended for use with non-localized strings, the /// overloads that take have been removed, and /// is used implicitly. /// internal static class Logger { /// /// The instance that is to be used /// by this static Logger for the duration of the appdomain. /// private static ILog facade = Create("DotNetOAuth"); #region ILog Members //// Although this static class doesn't literally implement the ILog interface, //// we implement (mostly) all the same methods in a static way. /// /// Gets a value indicating whether this logger is enabled for the level. /// /// /// true if this logger is enabled for events, false otherwise. /// /// /// /// This function is intended to lessen the computational cost of /// disabled log debug statements. /// /// For some ILog interface log, when you write: /// /// log.Debug("This is entry number: " + i ); /// /// /// You incur the cost constructing the message, string construction and concatenation in /// this case, regardless of whether the message is logged or not. /// /// /// If you are worried about speed (who isn't), then you should write: /// /// /// if (log.IsDebugEnabled) /// { /// log.Debug("This is entry number: " + i ); /// } /// /// /// This way you will not incur the cost of parameter /// construction if debugging is disabled for log. On /// the other hand, if the log is debug enabled, you /// will incur the cost of evaluating whether the logger is debug /// enabled twice. Once in and once in /// the . This is an insignificant overhead /// since evaluating a logger takes about 1% of the time it /// takes to actually log. This is the preferred style of logging. /// /// Alternatively if your logger is available statically then the is debug /// enabled state can be stored in a static variable like this: /// /// /// private static readonly bool isDebugEnabled = log.IsDebugEnabled; /// /// /// Then when you come to log you can write: /// /// /// if (isDebugEnabled) /// { /// log.Debug("This is entry number: " + i ); /// } /// /// /// This way the debug enabled state is only queried once /// when the class is loaded. Using a private static readonly /// variable is the most efficient because it is a run time constant /// and can be heavily optimized by the JIT compiler. /// /// /// Of course if you use a static readonly variable to /// hold the enabled state of the logger then you cannot /// change the enabled state at runtime to vary the logging /// that is produced. You have to decide if you need absolute /// speed or runtime flexibility. /// /// /// /// public static bool IsDebugEnabled { get { return facade.IsDebugEnabled; } } /// /// Gets a value indicating whether this logger is enabled for the level. /// /// /// true if this logger is enabled for events, false otherwise. /// /// /// For more information see . /// /// /// /// public static bool IsInfoEnabled { get { return facade.IsInfoEnabled; } } /// /// Gets a value indicating whether this logger is enabled for the level. /// /// /// true if this logger is enabled for events, false otherwise. /// /// /// For more information see . /// /// /// /// public static bool IsWarnEnabled { get { return facade.IsWarnEnabled; } } /// /// Gets a value indicating whether this logger is enabled for the level. /// /// /// true if this logger is enabled for events, false otherwise. /// /// /// For more information see . /// /// /// /// public static bool IsErrorEnabled { get { return facade.IsErrorEnabled; } } /// /// Gets a value indicating whether this logger is enabled for the level. /// /// /// true if this logger is enabled for events, false otherwise. /// /// /// For more information see . /// /// /// /// public static bool IsFatalEnabled { get { return facade.IsFatalEnabled; } } /// Log a message object with the level. /// /// Log a message object with the level. /// /// The message object to log. /// /// /// This method first checks if this logger is DEBUG /// enabled by comparing the level of this logger with the /// level. If this logger is /// DEBUG enabled, then it converts the message object /// (passed as parameter) to a string by invoking the appropriate /// . It then /// proceeds to call all the registered appenders in this logger /// and also higher in the hierarchy depending on the value of /// the additivity flag. /// /// WARNING Note that passing an /// to this method will print the name of the /// but no stack trace. To print a stack trace use the /// form instead. /// /// /// /// public static void Debug(object message) { facade.Debug(message); } /// /// Log a message object with the level including /// the stack trace of the passed /// as a parameter. /// /// The message object to log. /// The exception to log, including its stack trace. /// /// /// See the form for more detailed information. /// /// /// /// public static void Debug(object message, Exception exception) { facade.Debug(message, exception); } /// Log a formatted string with the level. /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object array containing zero or more objects to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void DebugFormat(string format, params object[] args) { facade.DebugFormat(CultureInfo.InvariantCulture, format, args); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void DebugFormat(string format, object arg0) { facade.DebugFormat(format, arg0); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void DebugFormat(string format, object arg0, object arg1) { facade.DebugFormat(format, arg0, arg1); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void DebugFormat(string format, object arg0, object arg1, object arg2) { facade.DebugFormat(format, arg0, arg1, arg2); } /* public static void DebugFormat(IFormatProvider provider, string format, params object[] args) { facade.DebugFormat(provider, format, args); } */ /// Log a message object with the level. /// /// Logs a message object with the level. /// /// /// /// This method first checks if this logger is INFO /// enabled by comparing the level of this logger with the /// level. If this logger is /// INFO enabled, then it converts the message object /// (passed as parameter) to a string by invoking the appropriate /// . It then /// proceeds to call all the registered appenders in this logger /// and also higher in the hierarchy depending on the value of the /// additivity flag. /// /// WARNING Note that passing an /// to this method will print the name of the /// but no stack trace. To print a stack trace use the /// form instead. /// /// /// The message object to log. /// /// public static void Info(object message) { facade.Info(message); } /// /// Logs a message object with the INFO level including /// the stack trace of the passed /// as a parameter. /// /// The message object to log. /// The exception to log, including its stack trace. /// /// /// See the form for more detailed information. /// /// /// /// public static void Info(object message, Exception exception) { facade.Info(message, exception); } /// Log a formatted message string with the level. /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object array containing zero or more objects to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void InfoFormat(string format, params object[] args) { facade.InfoFormat(CultureInfo.InvariantCulture, format, args); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void InfoFormat(string format, object arg0) { facade.InfoFormat(format, arg0); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void InfoFormat(string format, object arg0, object arg1) { facade.InfoFormat(format, arg0, arg1); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void InfoFormat(string format, object arg0, object arg1, object arg2) { facade.InfoFormat(format, arg0, arg1, arg2); } /* public static void InfoFormat(IFormatProvider provider, string format, params object[] args) { facade.InfoFormat(provider, format, args); } */ /// Log a message object with the level. /// /// Log a message object with the level. /// /// /// /// This method first checks if this logger is WARN /// enabled by comparing the level of this logger with the /// level. If this logger is /// WARN enabled, then it converts the message object /// (passed as parameter) to a string by invoking the appropriate /// . It then /// proceeds to call all the registered appenders in this logger /// and also higher in the hierarchy depending on the value of the /// additivity flag. /// /// WARNING Note that passing an /// to this method will print the name of the /// but no stack trace. To print a stack trace use the /// form instead. /// /// /// The message object to log. /// /// public static void Warn(object message) { facade.Warn(message); } /// /// Log a message object with the level including /// the stack trace of the passed /// as a parameter. /// /// The message object to log. /// The exception to log, including its stack trace. /// /// /// See the form for more detailed information. /// /// /// /// public static void Warn(object message, Exception exception) { facade.Warn(message, exception); } /// Log a formatted message string with the level. /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object array containing zero or more objects to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void WarnFormat(string format, params object[] args) { facade.WarnFormat(CultureInfo.InvariantCulture, format, args); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void WarnFormat(string format, object arg0) { facade.WarnFormat(format, arg0); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void WarnFormat(string format, object arg0, object arg1) { facade.WarnFormat(format, arg0, arg1); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void WarnFormat(string format, object arg0, object arg1, object arg2) { facade.WarnFormat(format, arg0, arg1, arg2); } /* public static void WarnFormat(IFormatProvider provider, string format, params object[] args) { facade.WarnFormat(provider, format, args); } */ /// Log a message object with the level. /// /// Logs a message object with the level. /// /// The message object to log. /// /// /// This method first checks if this logger is ERROR /// enabled by comparing the level of this logger with the /// level. If this logger is /// ERROR enabled, then it converts the message object /// (passed as parameter) to a string by invoking the appropriate /// . It then /// proceeds to call all the registered appenders in this logger /// and also higher in the hierarchy depending on the value of the /// additivity flag. /// /// WARNING Note that passing an /// to this method will print the name of the /// but no stack trace. To print a stack trace use the /// form instead. /// /// /// /// public static void Error(object message) { facade.Error(message); } /// /// Log a message object with the level including /// the stack trace of the passed /// as a parameter. /// /// The message object to log. /// The exception to log, including its stack trace. /// /// /// See the form for more detailed information. /// /// /// /// public static void Error(object message, Exception exception) { facade.Error(message, exception); } /// Log a formatted message string with the level. /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object array containing zero or more objects to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void ErrorFormat(string format, params object[] args) { facade.ErrorFormat(CultureInfo.InvariantCulture, format, args); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void ErrorFormat(string format, object arg0) { facade.ErrorFormat(format, arg0); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void ErrorFormat(string format, object arg0, object arg1) { facade.ErrorFormat(format, arg0, arg1); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void ErrorFormat(string format, object arg0, object arg1, object arg2) { facade.ErrorFormat(format, arg0, arg1, arg2); } /* public static void ErrorFormat(IFormatProvider provider, string format, params object[] args) { facade.ErrorFormat(provider, format, args); } */ /// Log a message object with the level. /// /// Log a message object with the level. /// /// /// /// This method first checks if this logger is FATAL /// enabled by comparing the level of this logger with the /// level. If this logger is /// FATAL enabled, then it converts the message object /// (passed as parameter) to a string by invoking the appropriate /// . It then /// proceeds to call all the registered appenders in this logger /// and also higher in the hierarchy depending on the value of the /// additivity flag. /// /// WARNING Note that passing an /// to this method will print the name of the /// but no stack trace. To print a stack trace use the /// form instead. /// /// /// The message object to log. /// /// public static void Fatal(object message) { facade.Fatal(message); } /// /// Log a message object with the level including /// the stack trace of the passed /// as a parameter. /// /// The message object to log. /// The exception to log, including its stack trace. /// /// /// See the form for more detailed information. /// /// /// /// public static void Fatal(object message, Exception exception) { facade.Fatal(message, exception); } /// Log a formatted message string with the level. /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object array containing zero or more objects to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void FatalFormat(string format, params object[] args) { facade.FatalFormat(CultureInfo.InvariantCulture, format, args); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void FatalFormat(string format, object arg0) { facade.FatalFormat(format, arg0); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void FatalFormat(string format, object arg0, object arg1) { facade.FatalFormat(format, arg0, arg1); } /// /// Logs a formatted message string with the level. /// /// A String containing zero or more format items /// An Object to format /// An Object to format /// An Object to format /// /// /// The message is formatted using the String.Format method. See /// for details of the syntax of the format string and the behavior /// of the formatting. /// /// /// This method does not take an object to include in the /// log event. To pass an use one of the /// methods instead. /// /// /// /// public static void FatalFormat(string format, object arg0, object arg1, object arg2) { facade.FatalFormat(format, arg0, arg1, arg2); } /* public static void FatalFormat(IFormatProvider provider, string format, params object[] args) { facade.FatalFormat(provider, format, args); } */ #endregion /// /// Creates an additional logger on demand for a subsection of the application. /// /// A name that will be included in the log file. /// The instance created with the given name. internal static ILog Create(string name) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } return InitializeFacade(name); } /// /// Creates an additional logger on demand for a subsection of the application. /// /// A type whose full name that will be included in the log file. /// The instance created with the given type name. internal static ILog Create(Type type) { if (type == null) { throw new ArgumentNullException("type"); } return Create(type.FullName); } /// /// Discovers the presence of Log4net.dll and other logging mechanisms /// and returns the best available logger. /// /// The name of the log to initialize. /// The instance of the logger to use. private static ILog InitializeFacade(string name) { ILog result = Log4NetLogger.Initialize(name) ?? TraceLogger.Initialize(name) ?? NoOpLogger.Initialize(); result.Info(Util.LibraryVersion); return result; } } }