?? hierarchy.cs
字號:
#region Copyright & License
//
// Copyright 2001-2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using log4net.ObjectRenderer;
using log4net.Core;
using log4net.Util;
namespace log4net.Repository.Hierarchy
{
#region LoggerCreationEvent
/// <summary>
/// Delegate used to handle logger creation event notifications.
/// </summary>
/// <param name="sender">The <see cref="Hierarchy"/> in which the <see cref="Logger"/> has been created.</param>
/// <param name="e">The <see cref="LoggerCreationEventArgs"/> event args that hold the <see cref="Logger"/> instance that has been created.</param>
/// <remarks>
/// <para>
/// Delegate used to handle logger creation event notifications.
/// </para>
/// </remarks>
public delegate void LoggerCreationEventHandler(object sender, LoggerCreationEventArgs e);
/// <summary>
/// Provides data for the <see cref="Hierarchy.LoggerCreatedEvent"/> event.
/// </summary>
/// <remarks>
/// <para>
/// A <see cref="Hierarchy.LoggerCreatedEvent"/> event is raised every time a
/// <see cref="Logger"/> is created.
/// </para>
/// </remarks>
public class LoggerCreationEventArgs : EventArgs
{
/// <summary>
/// The <see cref="Logger"/> created
/// </summary>
private Logger m_log;
/// <summary>
/// Constructor
/// </summary>
/// <param name="log">The <see cref="Logger"/> that has been created.</param>
/// <remarks>
/// <para>
/// Initializes a new instance of the <see cref="LoggerCreationEventArgs" /> event argument
/// class,with the specified <see cref="Logger"/>.
/// </para>
/// </remarks>
public LoggerCreationEventArgs(Logger log)
{
m_log = log;
}
/// <summary>
/// Gets the <see cref="Logger"/> that has been created.
/// </summary>
/// <value>
/// The <see cref="Logger"/> that has been created.
/// </value>
/// <remarks>
/// <para>
/// The <see cref="Logger"/> that has been created.
/// </para>
/// </remarks>
public Logger Logger
{
get { return m_log; }
}
}
#endregion LoggerCreationEvent
/// <summary>
/// Hierarchical organization of loggers
/// </summary>
/// <remarks>
/// <para>
/// <i>The casual user should not have to deal with this class
/// directly.</i>
/// </para>
/// <para>
/// This class is specialized in retrieving loggers by name and
/// also maintaining the logger hierarchy. Implements the
/// <see cref="ILoggerRepository"/> interface.
/// </para>
/// <para>
/// The structure of the logger hierarchy is maintained by the
/// <see cref="GetLogger(string)"/> method. The hierarchy is such that children
/// link to their parent but parents do not have any references to their
/// children. Moreover, loggers can be instantiated in any order, in
/// particular descendant before ancestor.
/// </para>
/// <para>
/// In case a descendant is created before a particular ancestor,
/// then it creates a provision node for the ancestor and adds itself
/// to the provision node. Other descendants of the same ancestor add
/// themselves to the previously created provision node.
/// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
public class Hierarchy : LoggerRepositorySkeleton, IBasicRepositoryConfigurator, IXmlRepositoryConfigurator
{
#region Public Events
/// <summary>
/// Event used to notify that a logger has been created.
/// </summary>
/// <remarks>
/// <para>
/// Event raised when a logger is created.
/// </para>
/// </remarks>
public event LoggerCreationEventHandler LoggerCreatedEvent
{
add { m_loggerCreatedEvent += value; }
remove { m_loggerCreatedEvent -= value; }
}
#endregion Public Events
#region Public Instance Constructors
/// <summary>
/// Default constructor
/// </summary>
/// <remarks>
/// <para>
/// Initializes a new instance of the <see cref="Hierarchy" /> class.
/// </para>
/// </remarks>
public Hierarchy() : this(new DefaultLoggerFactory())
{
}
/// <summary>
/// Construct with properties
/// </summary>
/// <param name="properties">The properties to pass to this repository.</param>
/// <remarks>
/// <para>
/// Initializes a new instance of the <see cref="Hierarchy" /> class.
/// </para>
/// </remarks>
public Hierarchy(PropertiesDictionary properties) : this(properties, new DefaultLoggerFactory())
{
}
/// <summary>
/// Construct with a logger factory
/// </summary>
/// <param name="loggerFactory">The factory to use to create new logger instances.</param>
/// <remarks>
/// <para>
/// Initializes a new instance of the <see cref="Hierarchy" /> class with
/// the specified <see cref="ILoggerFactory" />.
/// </para>
/// </remarks>
public Hierarchy(ILoggerFactory loggerFactory) : this(new PropertiesDictionary(), loggerFactory)
{
}
/// <summary>
/// Construct with properties and a logger factory
/// </summary>
/// <param name="properties">The properties to pass to this repository.</param>
/// <param name="loggerFactory">The factory to use to create new logger instances.</param>
/// <remarks>
/// <para>
/// Initializes a new instance of the <see cref="Hierarchy" /> class with
/// the specified <see cref="ILoggerFactory" />.
/// </para>
/// </remarks>
public Hierarchy(PropertiesDictionary properties, ILoggerFactory loggerFactory) : base(properties)
{
if (loggerFactory == null)
{
throw new ArgumentNullException("loggerFactory");
}
m_defaultFactory = loggerFactory;
m_ht = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
}
#endregion Public Instance Constructors
#region Public Instance Properties
/// <summary>
/// Has no appender warning been emitted
/// </summary>
/// <remarks>
/// <para>
/// Flag to indicate if we have already issued a warning
/// about not having an appender warning.
/// </para>
/// </remarks>
public bool EmittedNoAppenderWarning
{
get { return m_emittedNoAppenderWarning; }
set { m_emittedNoAppenderWarning = value; }
}
/// <summary>
/// Get the root of this hierarchy
/// </summary>
/// <remarks>
/// <para>
/// Get the root of this hierarchy.
/// </para>
/// </remarks>
public Logger Root
{
get
{
if (m_root == null)
{
lock(this)
{
if (m_root == null)
{
// Create the root logger
Logger root = m_defaultFactory.CreateLogger(null);
root.Hierarchy = this;
// Store root
m_root = root;
}
}
}
return m_root;
}
}
/// <summary>
/// Gets or sets the default <see cref="ILoggerFactory" /> instance.
/// </summary>
/// <value>The default <see cref="ILoggerFactory" /></value>
/// <remarks>
/// <para>
/// The logger factory is used to create logger instances.
/// </para>
/// </remarks>
public ILoggerFactory LoggerFactory
{
get { return m_defaultFactory; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
m_defaultFactory = value;
}
}
#endregion Public Instance Properties
#region Override Implementation of LoggerRepositorySkeleton
/// <summary>
/// Test if a logger exists
/// </summary>
/// <param name="name">The name of the logger to lookup</param>
/// <returns>The Logger object with the name specified</returns>
/// <remarks>
/// <para>
/// Check if the named logger exists in the hierarchy. If so return
/// its reference, otherwise returns <c>null</c>.
/// </para>
/// </remarks>
override public ILogger Exists(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
return m_ht[new LoggerKey(name)] as Logger;
}
/// <summary>
/// Returns all the currently defined loggers in the hierarchy as an Array
/// </summary>
/// <returns>All the defined loggers</returns>
/// <remarks>
/// <para>
/// Returns all the currently defined loggers in the hierarchy as an Array.
/// The root logger is <b>not</b> included in the returned
/// enumeration.
/// </para>
/// </remarks>
override public ILogger[] GetCurrentLoggers()
{
// The accumulation in loggers is necessary because not all elements in
// ht are Logger objects as there might be some ProvisionNodes
// as well.
System.Collections.ArrayList loggers = new System.Collections.ArrayList(m_ht.Count);
// Iterate through m_ht values
foreach(object node in m_ht.Values)
{
if (node is Logger)
{
loggers.Add(node);
}
}
return (Logger[])loggers.ToArray(typeof(Logger));
}
/// <summary>
/// Return a new logger instance named as the first parameter using
/// the default factory.
/// </summary>
/// <remarks>
/// <para>
/// Return a new logger instance named as the first parameter using
/// the default factory.
/// </para>
/// <para>
/// If a logger of that name already exists, then it will be
/// returned. Otherwise, a new logger will be instantiated and
/// then linked with its existing ancestors as well as children.
/// </para>
/// </remarks>
/// <param name="name">The name of the logger to retrieve</param>
/// <returns>The logger object with the name specified</returns>
override public ILogger GetLogger(string name)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
return GetLogger(name, m_defaultFactory);
}
/// <summary>
/// Shutting down a hierarchy will <i>safely</i> close and remove
/// all appenders in all loggers including the root logger.
/// </summary>
/// <remarks>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -