#region Apache License // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to you 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.Core; using log4net.Appender; namespace log4net.Util { /// /// A straightforward implementation of the interface. /// /// /// /// This is the default implementation of the /// interface. Implementors of the interface /// should aggregate an instance of this type. /// /// /// Nicko Cadell /// Gert Driesen public class AppenderAttachedImpl : IAppenderAttachable { #region Public Instance Constructors /// /// Constructor /// /// /// /// Initializes a new instance of the class. /// /// public AppenderAttachedImpl() { } #endregion Public Instance Constructors #region Public Instance Methods /// /// Append on on all attached appenders. /// /// The event being logged. /// The number of appenders called. /// /// /// Calls the method on all /// attached appenders. /// /// public int AppendLoopOnAppenders(LoggingEvent loggingEvent) { if (loggingEvent == null) { throw new ArgumentNullException("loggingEvent"); } // m_appenderList is null when empty if (m_appenderList == null) { return 0; } if (m_appenderArray == null) { m_appenderArray = m_appenderList.ToArray(); } foreach(IAppender appender in m_appenderArray) { try { appender.DoAppend(loggingEvent); } catch(Exception ex) { LogLog.Error(declaringType, "Failed to append to appender [" + appender.Name + "]", ex); } } return m_appenderList.Count; } /// /// Append on on all attached appenders. /// /// The array of events being logged. /// The number of appenders called. /// /// /// Calls the method on all /// attached appenders. /// /// public int AppendLoopOnAppenders(LoggingEvent[] loggingEvents) { if (loggingEvents == null) { throw new ArgumentNullException("loggingEvents"); } if (loggingEvents.Length == 0) { throw new ArgumentException("loggingEvents array must not be empty", "loggingEvents"); } if (loggingEvents.Length == 1) { // Fall back to single event path return AppendLoopOnAppenders(loggingEvents[0]); } // m_appenderList is null when empty if (m_appenderList == null) { return 0; } if (m_appenderArray == null) { m_appenderArray = m_appenderList.ToArray(); } foreach(IAppender appender in m_appenderArray) { try { CallAppend(appender, loggingEvents); } catch(Exception ex) { LogLog.Error(declaringType, "Failed to append to appender [" + appender.Name + "]", ex); } } return m_appenderList.Count; } #endregion Public Instance Methods #region Private Static Methods /// /// Calls the DoAppende method on the with /// the objects supplied. /// /// The appender /// The events /// /// /// If the supports the /// interface then the will be passed /// through using that interface. Otherwise the /// objects in the array will be passed one at a time. /// /// private static void CallAppend(IAppender appender, LoggingEvent[] loggingEvents) { IBulkAppender bulkAppender = appender as IBulkAppender; if (bulkAppender != null) { bulkAppender.DoAppend(loggingEvents); } else { foreach(LoggingEvent loggingEvent in loggingEvents) { appender.DoAppend(loggingEvent); } } } #endregion #region Implementation of IAppenderAttachable /// /// Attaches an appender. /// /// The appender to add. /// /// /// If the appender is already in the list it won't be added again. /// /// public void AddAppender(IAppender newAppender) { // Null values for newAppender parameter are strictly forbidden. if (newAppender == null) { throw new ArgumentNullException("newAppender"); } m_appenderArray = null; if (m_appenderList == null) { m_appenderList = new AppenderCollection(1); } if (!m_appenderList.Contains(newAppender)) { m_appenderList.Add(newAppender); } } /// /// Gets all attached appenders. /// /// /// A collection of attached appenders, or null if there /// are no attached appenders. /// /// /// /// The read only collection of all currently attached appenders. /// /// public AppenderCollection Appenders { get { if (m_appenderList == null) { // We must always return a valid collection return AppenderCollection.EmptyCollection; } else { return AppenderCollection.ReadOnly(m_appenderList); } } } /// /// Gets an attached appender with the specified name. /// /// The name of the appender to get. /// /// The appender with the name specified, or null if no appender with the /// specified name is found. /// /// /// /// Lookup an attached appender by name. /// /// public IAppender GetAppender(string name) { if (m_appenderList != null && name != null) { foreach(IAppender appender in m_appenderList) { if (name == appender.Name) { return appender; } } } return null; } /// /// Removes all attached appenders. /// /// /// /// Removes and closes all attached appenders /// /// public void RemoveAllAppenders() { if (m_appenderList != null) { foreach(IAppender appender in m_appenderList) { try { appender.Close(); } catch(Exception ex) { LogLog.Error(declaringType, "Failed to Close appender ["+appender.Name+"]", ex); } } m_appenderList = null; m_appenderArray = null; } } /// /// Removes the specified appender from the list of attached appenders. /// /// The appender to remove. /// The appender removed from the list /// /// /// The appender removed is not closed. /// If you are discarding the appender you must call /// on the appender removed. /// /// public IAppender RemoveAppender(IAppender appender) { if (appender != null && m_appenderList != null) { m_appenderList.Remove(appender); if (m_appenderList.Count == 0) { m_appenderList = null; } m_appenderArray = null; } return appender; } /// /// Removes the appender with the specified name from the list of appenders. /// /// The name of the appender to remove. /// The appender removed from the list /// /// /// The appender removed is not closed. /// If you are discarding the appender you must call /// on the appender removed. /// /// public IAppender RemoveAppender(string name) { return RemoveAppender(GetAppender(name)); } #endregion #region Private Instance Fields /// /// List of appenders /// private AppenderCollection m_appenderList; /// /// Array of appenders, used to cache the m_appenderList /// private IAppender[] m_appenderArray; #endregion Private Instance Fields #region Private Static Fields /// /// The fully qualified type of the AppenderAttachedImpl class. /// /// /// Used by the internal logger to record the Type of the /// log message. /// private readonly static Type declaringType = typeof(AppenderAttachedImpl); #endregion Private Static Fields } }