ForwardingAppender.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using log4net.Util;
  21. using log4net.Layout;
  22. using log4net.Core;
  23. namespace log4net.Appender
  24. {
  25. /// <summary>
  26. /// This appender forwards logging events to attached appenders.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// The forwarding appender can be used to specify different thresholds
  31. /// and filters for the same appender at different locations within the hierarchy.
  32. /// </para>
  33. /// </remarks>
  34. /// <author>Nicko Cadell</author>
  35. /// <author>Gert Driesen</author>
  36. public class ForwardingAppender : AppenderSkeleton, IAppenderAttachable
  37. {
  38. #region Public Instance Constructors
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="ForwardingAppender" /> class.
  41. /// </summary>
  42. /// <remarks>
  43. /// <para>
  44. /// Default constructor.
  45. /// </para>
  46. /// </remarks>
  47. public ForwardingAppender()
  48. {
  49. }
  50. #endregion Public Instance Constructors
  51. #region Override implementation of AppenderSkeleton
  52. /// <summary>
  53. /// Closes the appender and releases resources.
  54. /// </summary>
  55. /// <remarks>
  56. /// <para>
  57. /// Releases any resources allocated within the appender such as file handles,
  58. /// network connections, etc.
  59. /// </para>
  60. /// <para>
  61. /// It is a programming error to append to a closed appender.
  62. /// </para>
  63. /// </remarks>
  64. override protected void OnClose()
  65. {
  66. // Remove all the attached appenders
  67. lock(this)
  68. {
  69. if (m_appenderAttachedImpl != null)
  70. {
  71. m_appenderAttachedImpl.RemoveAllAppenders();
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Forward the logging event to the attached appenders
  77. /// </summary>
  78. /// <param name="loggingEvent">The event to log.</param>
  79. /// <remarks>
  80. /// <para>
  81. /// Delivers the logging event to all the attached appenders.
  82. /// </para>
  83. /// </remarks>
  84. override protected void Append(LoggingEvent loggingEvent)
  85. {
  86. // Pass the logging event on the the attached appenders
  87. if (m_appenderAttachedImpl != null)
  88. {
  89. m_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvent);
  90. }
  91. }
  92. /// <summary>
  93. /// Forward the logging events to the attached appenders
  94. /// </summary>
  95. /// <param name="loggingEvents">The array of events to log.</param>
  96. /// <remarks>
  97. /// <para>
  98. /// Delivers the logging events to all the attached appenders.
  99. /// </para>
  100. /// </remarks>
  101. override protected void Append(LoggingEvent[] loggingEvents)
  102. {
  103. // Pass the logging event on the the attached appenders
  104. if (m_appenderAttachedImpl != null)
  105. {
  106. m_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvents);
  107. }
  108. }
  109. #endregion Override implementation of AppenderSkeleton
  110. #region Implementation of IAppenderAttachable
  111. /// <summary>
  112. /// Adds an <see cref="IAppender" /> to the list of appenders of this
  113. /// instance.
  114. /// </summary>
  115. /// <param name="newAppender">The <see cref="IAppender" /> to add to this appender.</param>
  116. /// <remarks>
  117. /// <para>
  118. /// If the specified <see cref="IAppender" /> is already in the list of
  119. /// appenders, then it won't be added again.
  120. /// </para>
  121. /// </remarks>
  122. virtual public void AddAppender(IAppender newAppender)
  123. {
  124. if (newAppender == null)
  125. {
  126. throw new ArgumentNullException("newAppender");
  127. }
  128. lock(this)
  129. {
  130. if (m_appenderAttachedImpl == null)
  131. {
  132. m_appenderAttachedImpl = new log4net.Util.AppenderAttachedImpl();
  133. }
  134. m_appenderAttachedImpl.AddAppender(newAppender);
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the appenders contained in this appender as an
  139. /// <see cref="System.Collections.ICollection"/>.
  140. /// </summary>
  141. /// <remarks>
  142. /// If no appenders can be found, then an <see cref="EmptyCollection"/>
  143. /// is returned.
  144. /// </remarks>
  145. /// <returns>
  146. /// A collection of the appenders in this appender.
  147. /// </returns>
  148. virtual public AppenderCollection Appenders
  149. {
  150. get
  151. {
  152. lock(this)
  153. {
  154. if (m_appenderAttachedImpl == null)
  155. {
  156. return AppenderCollection.EmptyCollection;
  157. }
  158. else
  159. {
  160. return m_appenderAttachedImpl.Appenders;
  161. }
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// Looks for the appender with the specified name.
  167. /// </summary>
  168. /// <param name="name">The name of the appender to lookup.</param>
  169. /// <returns>
  170. /// The appender with the specified name, or <c>null</c>.
  171. /// </returns>
  172. /// <remarks>
  173. /// <para>
  174. /// Get the named appender attached to this appender.
  175. /// </para>
  176. /// </remarks>
  177. virtual public IAppender GetAppender(string name)
  178. {
  179. lock(this)
  180. {
  181. if (m_appenderAttachedImpl == null || name == null)
  182. {
  183. return null;
  184. }
  185. return m_appenderAttachedImpl.GetAppender(name);
  186. }
  187. }
  188. /// <summary>
  189. /// Removes all previously added appenders from this appender.
  190. /// </summary>
  191. /// <remarks>
  192. /// <para>
  193. /// This is useful when re-reading configuration information.
  194. /// </para>
  195. /// </remarks>
  196. virtual public void RemoveAllAppenders()
  197. {
  198. lock(this)
  199. {
  200. if (m_appenderAttachedImpl != null)
  201. {
  202. m_appenderAttachedImpl.RemoveAllAppenders();
  203. m_appenderAttachedImpl = null;
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Removes the specified appender from the list of appenders.
  209. /// </summary>
  210. /// <param name="appender">The appender to remove.</param>
  211. /// <returns>The appender removed from the list</returns>
  212. /// <remarks>
  213. /// The appender removed is not closed.
  214. /// If you are discarding the appender you must call
  215. /// <see cref="IAppender.Close"/> on the appender removed.
  216. /// </remarks>
  217. virtual public IAppender RemoveAppender(IAppender appender)
  218. {
  219. lock(this)
  220. {
  221. if (appender != null && m_appenderAttachedImpl != null)
  222. {
  223. return m_appenderAttachedImpl.RemoveAppender(appender);
  224. }
  225. }
  226. return null;
  227. }
  228. /// <summary>
  229. /// Removes the appender with the specified name from the list of appenders.
  230. /// </summary>
  231. /// <param name="name">The name of the appender to remove.</param>
  232. /// <returns>The appender removed from the list</returns>
  233. /// <remarks>
  234. /// The appender removed is not closed.
  235. /// If you are discarding the appender you must call
  236. /// <see cref="IAppender.Close"/> on the appender removed.
  237. /// </remarks>
  238. virtual public IAppender RemoveAppender(string name)
  239. {
  240. lock(this)
  241. {
  242. if (name != null && m_appenderAttachedImpl != null)
  243. {
  244. return m_appenderAttachedImpl.RemoveAppender(name);
  245. }
  246. }
  247. return null;
  248. }
  249. #endregion Implementation of IAppenderAttachable
  250. #region Private Instance Fields
  251. /// <summary>
  252. /// Implementation of the <see cref="IAppenderAttachable"/> interface
  253. /// </summary>
  254. private AppenderAttachedImpl m_appenderAttachedImpl;
  255. #endregion Private Instance Fields
  256. }
  257. }