// // Copyright (c) 2004-2006 Jaroslaw Kowalski // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * Neither the name of Jaroslaw Kowalski nor the names of its // contributors may be used to endorse or promote products derived from this // software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // THE POSSIBILITY OF SUCH DAMAGE. // using System; using System.Collections; using System.Xml; using System.IO; using System.Reflection; using System.Threading; using System.Diagnostics; using System.Security; using System.Text; using System.Runtime.CompilerServices; using System.Globalization; using NLog.Config; using NLog.Internal; using NLog.Targets; namespace NLog { /// /// Creates and manages instances of objects. /// public sealed class LogManager { private static LogFactory _globalFactory = new LogFactory(); /// /// Occurs when logging changes. /// public static event LoggingConfigurationChanged ConfigurationChanged { add { _globalFactory.ConfigurationChanged += value; } remove { _globalFactory.ConfigurationChanged -= value; } } #if !NETCF /// /// Occurs when logging gets reloaded. /// public static event LoggingConfigurationReloaded ConfigurationReloaded { add { _globalFactory.ConfigurationReloaded += value; } remove { _globalFactory.ConfigurationReloaded -= value; } } #endif /// /// Specified whether NLog should throw exceptions. By default exceptions /// are not thrown under any circumstances. /// public static bool ThrowExceptions { get { return _globalFactory.ThrowExceptions; } set { _globalFactory.ThrowExceptions = value; } } private LogManager(){} #if !NETCF /// /// Gets the logger named after the currently-being-initialized class. /// /// The logger. /// This is a slow-running method. /// Make sure you're not doing this in a loop. [MethodImpl(MethodImplOptions.NoInlining)] public static Logger GetCurrentClassLogger() { StackFrame frame = new StackFrame(1, false); return _globalFactory.GetLogger(frame.GetMethod().DeclaringType.FullName); } /// /// Gets the logger named after the currently-being-initialized class. /// /// the logger class. The class must inherit from /// The logger. /// This is a slow-running method. /// Make sure you're not doing this in a loop. [MethodImpl(MethodImplOptions.NoInlining)] public static Logger GetCurrentClassLogger(Type loggerType) { StackFrame frame = new StackFrame(1, false); return _globalFactory.GetLogger(frame.GetMethod().DeclaringType.FullName, loggerType); } #endif /// /// Creates a logger that discards all log messages. /// /// public static Logger CreateNullLogger() { return _globalFactory.CreateNullLogger(); } /// /// Gets the specified named logger. /// /// name of the logger /// The logger reference. Multiple calls to GetLogger with the same argument aren't guaranteed to return the same logger reference. public static Logger GetLogger(string name) { return _globalFactory.GetLogger(name); } /// /// Gets the specified named logger. /// /// name of the logger /// the logger class. The class must inherit from /// The logger reference. Multiple calls to GetLogger with the same argument aren't guaranteed to return the same logger reference. public static Logger GetLogger(string name, Type loggerType) { return _globalFactory.GetLogger(name, loggerType); } /// /// Gets or sets the current logging configuration. /// public static LoggingConfiguration Configuration { get { return _globalFactory.Configuration; } set { _globalFactory.Configuration = value; } } /// /// Loops through all loggers previously returned by GetLogger. /// and recalculates their target and filter list. Useful after modifying the configuration programmatically /// to ensure that all loggers have been properly configured. /// public static void ReconfigExistingLoggers() { _globalFactory.ReconfigExistingLoggers(); } /// /// Flush any pending log messages (in case of asynchronous targets). /// public static void Flush() { _globalFactory.Flush(); } /// /// Flush any pending log messages (in case of asynchronous targets). /// /// Maximum time to allow for the flush. Any messages after that time will be discarded. public static void Flush(TimeSpan timeout) { _globalFactory.Flush(timeout); } /// /// Flush any pending log messages (in case of asynchronous targets). /// /// Maximum time to allow for the flush. Any messages after that time will be discarded. public static void Flush(int timeoutMilliseconds) { _globalFactory.Flush(timeoutMilliseconds); } /// Decreases the log enable counter and if it reaches -1 /// the logs are disabled. /// Logging is enabled if the number of calls is greater /// than or equal to calls. /// An object that iplements IDisposable whose Dispose() method /// reenables logging. To be used with C# using () statement. public static IDisposable DisableLogging() { return _globalFactory.DisableLogging(); } /// Increases the log enable counter and if it reaches 0 the logs are disabled. /// Logging is enabled if the number of calls is greater /// than or equal to calls. public static void EnableLogging() { _globalFactory.EnableLogging(); } /// /// Returns if logging is currently enabled. /// /// if logging is currently enabled, /// otherwise. /// Logging is enabled if the number of calls is greater /// than or equal to calls. public static bool IsLoggingEnabled() { return _globalFactory.IsLoggingEnabled(); } /// /// Global log threshold. Log events below this threshold are not logged. /// public static LogLevel GlobalThreshold { get { return _globalFactory.GlobalThreshold; } set { _globalFactory.GlobalThreshold = value; } } #if !NETCF private static void SetupTerminationEvents() { AppDomain.CurrentDomain.ProcessExit += new EventHandler(TurnOffLogging); AppDomain.CurrentDomain.DomainUnload += new EventHandler(TurnOffLogging); } private static void TurnOffLogging(object sender, EventArgs args) { // reset logging configuration to null // this causes old configuration (if any) to be closed. InternalLogger.Info("Shutting down logging..."); Configuration = null; InternalLogger.Info("Logger has been shut down."); } #endif static LogManager() { #if !NETCF try { SetupTerminationEvents(); } catch (Exception ex) { InternalLogger.Warn("Error setting up termiation events: {0}", ex); } #endif } } }