BufferingForwardingAppender.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /// Buffers events and then forwards them to attached appenders.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// The events are buffered in this appender until conditions are
  31. /// met to allow the appender to deliver the events to the attached
  32. /// appenders. See <see cref="BufferingAppenderSkeleton"/> for the
  33. /// conditions that cause the buffer to be sent.
  34. /// </para>
  35. /// <para>The forwarding appender can be used to specify different
  36. /// thresholds and filters for the same appender at different locations
  37. /// within the hierarchy.
  38. /// </para>
  39. /// </remarks>
  40. /// <author>Nicko Cadell</author>
  41. /// <author>Gert Driesen</author>
  42. public class BufferingForwardingAppender : BufferingAppenderSkeleton, IAppenderAttachable
  43. {
  44. #region Public Instance Constructors
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="BufferingForwardingAppender" /> class.
  47. /// </summary>
  48. /// <remarks>
  49. /// <para>
  50. /// Default constructor.
  51. /// </para>
  52. /// </remarks>
  53. public BufferingForwardingAppender()
  54. {
  55. }
  56. #endregion Public Instance Constructors
  57. #region Override implementation of AppenderSkeleton
  58. /// <summary>
  59. /// Closes the appender and releases resources.
  60. /// </summary>
  61. /// <remarks>
  62. /// <para>
  63. /// Releases any resources allocated within the appender such as file handles,
  64. /// network connections, etc.
  65. /// </para>
  66. /// <para>
  67. /// It is a programming error to append to a closed appender.
  68. /// </para>
  69. /// </remarks>
  70. override protected void OnClose()
  71. {
  72. // Remove all the attached appenders
  73. lock(this)
  74. {
  75. // Delegate to base, which will flush buffers
  76. base.OnClose();
  77. if (m_appenderAttachedImpl != null)
  78. {
  79. m_appenderAttachedImpl.RemoveAllAppenders();
  80. }
  81. }
  82. }
  83. #endregion Override implementation of AppenderSkeleton
  84. #region Override implementation of BufferingAppenderSkeleton
  85. /// <summary>
  86. /// Send the events.
  87. /// </summary>
  88. /// <param name="events">The events that need to be send.</param>
  89. /// <remarks>
  90. /// <para>
  91. /// Forwards the events to the attached appenders.
  92. /// </para>
  93. /// </remarks>
  94. override protected void SendBuffer(LoggingEvent[] events)
  95. {
  96. // Pass the logging event on to the attached appenders
  97. if (m_appenderAttachedImpl != null)
  98. {
  99. m_appenderAttachedImpl.AppendLoopOnAppenders(events);
  100. }
  101. }
  102. #endregion Override implementation of BufferingAppenderSkeleton
  103. #region Implementation of IAppenderAttachable
  104. /// <summary>
  105. /// Adds an <see cref="IAppender" /> to the list of appenders of this
  106. /// instance.
  107. /// </summary>
  108. /// <param name="newAppender">The <see cref="IAppender" /> to add to this appender.</param>
  109. /// <remarks>
  110. /// <para>
  111. /// If the specified <see cref="IAppender" /> is already in the list of
  112. /// appenders, then it won't be added again.
  113. /// </para>
  114. /// </remarks>
  115. virtual public void AddAppender(IAppender newAppender)
  116. {
  117. if (newAppender == null)
  118. {
  119. throw new ArgumentNullException("newAppender");
  120. }
  121. lock(this)
  122. {
  123. if (m_appenderAttachedImpl == null)
  124. {
  125. m_appenderAttachedImpl = new log4net.Util.AppenderAttachedImpl();
  126. }
  127. m_appenderAttachedImpl.AddAppender(newAppender);
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the appenders contained in this appender as an
  132. /// <see cref="System.Collections.ICollection"/>.
  133. /// </summary>
  134. /// <remarks>
  135. /// If no appenders can be found, then an <see cref="EmptyCollection"/>
  136. /// is returned.
  137. /// </remarks>
  138. /// <returns>
  139. /// A collection of the appenders in this appender.
  140. /// </returns>
  141. virtual public AppenderCollection Appenders
  142. {
  143. get
  144. {
  145. lock(this)
  146. {
  147. if (m_appenderAttachedImpl == null)
  148. {
  149. return AppenderCollection.EmptyCollection;
  150. }
  151. else
  152. {
  153. return m_appenderAttachedImpl.Appenders;
  154. }
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Looks for the appender with the specified name.
  160. /// </summary>
  161. /// <param name="name">The name of the appender to lookup.</param>
  162. /// <returns>
  163. /// The appender with the specified name, or <c>null</c>.
  164. /// </returns>
  165. /// <remarks>
  166. /// <para>
  167. /// Get the named appender attached to this buffering appender.
  168. /// </para>
  169. /// </remarks>
  170. virtual public IAppender GetAppender(string name)
  171. {
  172. lock(this)
  173. {
  174. if (m_appenderAttachedImpl == null || name == null)
  175. {
  176. return null;
  177. }
  178. return m_appenderAttachedImpl.GetAppender(name);
  179. }
  180. }
  181. /// <summary>
  182. /// Removes all previously added appenders from this appender.
  183. /// </summary>
  184. /// <remarks>
  185. /// <para>
  186. /// This is useful when re-reading configuration information.
  187. /// </para>
  188. /// </remarks>
  189. virtual public void RemoveAllAppenders()
  190. {
  191. lock(this)
  192. {
  193. if (m_appenderAttachedImpl != null)
  194. {
  195. m_appenderAttachedImpl.RemoveAllAppenders();
  196. m_appenderAttachedImpl = null;
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Removes the specified appender from the list of appenders.
  202. /// </summary>
  203. /// <param name="appender">The appender to remove.</param>
  204. /// <returns>The appender removed from the list</returns>
  205. /// <remarks>
  206. /// The appender removed is not closed.
  207. /// If you are discarding the appender you must call
  208. /// <see cref="IAppender.Close"/> on the appender removed.
  209. /// </remarks>
  210. virtual public IAppender RemoveAppender(IAppender appender)
  211. {
  212. lock(this)
  213. {
  214. if (appender != null && m_appenderAttachedImpl != null)
  215. {
  216. return m_appenderAttachedImpl.RemoveAppender(appender);
  217. }
  218. }
  219. return null;
  220. }
  221. /// <summary>
  222. /// Removes the appender with the specified name from the list of appenders.
  223. /// </summary>
  224. /// <param name="name">The name of the appender to remove.</param>
  225. /// <returns>The appender removed from the list</returns>
  226. /// <remarks>
  227. /// The appender removed is not closed.
  228. /// If you are discarding the appender you must call
  229. /// <see cref="IAppender.Close"/> on the appender removed.
  230. /// </remarks>
  231. virtual public IAppender RemoveAppender(string name)
  232. {
  233. lock(this)
  234. {
  235. if (name != null && m_appenderAttachedImpl != null)
  236. {
  237. return m_appenderAttachedImpl.RemoveAppender(name);
  238. }
  239. }
  240. return null;
  241. }
  242. #endregion Implementation of IAppenderAttachable
  243. #region Private Instance Fields
  244. /// <summary>
  245. /// Implementation of the <see cref="IAppenderAttachable"/> interface
  246. /// </summary>
  247. private AppenderAttachedImpl m_appenderAttachedImpl;
  248. #endregion Private Instance Fields
  249. }
  250. }