MemoryAppender.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 System.Collections;
  21. using log4net.Core;
  22. namespace log4net.Appender
  23. {
  24. /// <summary>
  25. /// Stores logging events in an array.
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// The memory appender stores all the logging events
  30. /// that are appended in an in-memory array.
  31. /// </para>
  32. /// <para>
  33. /// Use the <see cref="M:PopAllEvents()"/> method to get
  34. /// and clear the current list of events that have been appended.
  35. /// </para>
  36. /// <para>
  37. /// Use the <see cref="M:GetEvents()"/> method to get the current
  38. /// list of events that have been appended. Note there is a
  39. /// race-condition when calling <see cref="M:GetEvents()"/> and
  40. /// <see cref="M:Clear()"/> in pairs, you better use <see
  41. /// mref="M:PopAllEvents()"/> in that case.
  42. /// </para>
  43. /// <para>
  44. /// Use the <see cref="M:Clear()"/> method to clear the
  45. /// current list of events. Note there is a
  46. /// race-condition when calling <see cref="M:GetEvents()"/> and
  47. /// <see cref="M:Clear()"/> in pairs, you better use <see
  48. /// mref="M:PopAllEvents()"/> in that case.
  49. /// </para>
  50. /// </remarks>
  51. /// <author>Julian Biddle</author>
  52. /// <author>Nicko Cadell</author>
  53. /// <author>Gert Driesen</author>
  54. public class MemoryAppender : AppenderSkeleton
  55. {
  56. #region Public Instance Constructors
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="MemoryAppender" /> class.
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>
  62. /// Default constructor.
  63. /// </para>
  64. /// </remarks>
  65. public MemoryAppender() : base()
  66. {
  67. m_eventsList = new ArrayList();
  68. }
  69. #endregion Protected Instance Constructors
  70. #region Public Instance Properties
  71. /// <summary>
  72. /// Gets the events that have been logged.
  73. /// </summary>
  74. /// <returns>The events that have been logged</returns>
  75. /// <remarks>
  76. /// <para>
  77. /// Gets the events that have been logged.
  78. /// </para>
  79. /// </remarks>
  80. virtual public LoggingEvent[] GetEvents()
  81. {
  82. lock (m_eventsList.SyncRoot)
  83. {
  84. return (LoggingEvent[]) m_eventsList.ToArray(typeof(LoggingEvent));
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets a value indicating whether only part of the logging event
  89. /// data should be fixed.
  90. /// </summary>
  91. /// <value>
  92. /// <c>true</c> if the appender should only fix part of the logging event
  93. /// data, otherwise <c>false</c>. The default is <c>false</c>.
  94. /// </value>
  95. /// <remarks>
  96. /// <para>
  97. /// Setting this property to <c>true</c> will cause only part of the event
  98. /// data to be fixed and stored in the appender, hereby improving performance.
  99. /// </para>
  100. /// <para>
  101. /// See <see cref="M:LoggingEvent.FixVolatileData(bool)"/> for more information.
  102. /// </para>
  103. /// </remarks>
  104. [Obsolete("Use Fix property")]
  105. virtual public bool OnlyFixPartialEventData
  106. {
  107. get { return (Fix == FixFlags.Partial); }
  108. set
  109. {
  110. if (value)
  111. {
  112. Fix = FixFlags.Partial;
  113. }
  114. else
  115. {
  116. Fix = FixFlags.All;
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Gets or sets the fields that will be fixed in the event
  122. /// </summary>
  123. /// <remarks>
  124. /// <para>
  125. /// The logging event needs to have certain thread specific values
  126. /// captured before it can be buffered. See <see cref="LoggingEvent.Fix"/>
  127. /// for details.
  128. /// </para>
  129. /// </remarks>
  130. virtual public FixFlags Fix
  131. {
  132. get { return m_fixFlags; }
  133. set { m_fixFlags = value; }
  134. }
  135. #endregion Public Instance Properties
  136. #region Override implementation of AppenderSkeleton
  137. /// <summary>
  138. /// This method is called by the <see cref="M:AppenderSkeleton.DoAppend(LoggingEvent)"/> method.
  139. /// </summary>
  140. /// <param name="loggingEvent">the event to log</param>
  141. /// <remarks>
  142. /// <para>Stores the <paramref name="loggingEvent"/> in the events list.</para>
  143. /// </remarks>
  144. override protected void Append(LoggingEvent loggingEvent)
  145. {
  146. // Because we are caching the LoggingEvent beyond the
  147. // lifetime of the Append() method we must fix any
  148. // volatile data in the event.
  149. loggingEvent.Fix = this.Fix;
  150. lock (m_eventsList.SyncRoot)
  151. {
  152. m_eventsList.Add(loggingEvent);
  153. }
  154. }
  155. #endregion Override implementation of AppenderSkeleton
  156. #region Public Instance Methods
  157. /// <summary>
  158. /// Clear the list of events
  159. /// </summary>
  160. /// <remarks>
  161. /// Clear the list of events
  162. /// </remarks>
  163. virtual public void Clear()
  164. {
  165. lock (m_eventsList.SyncRoot)
  166. {
  167. m_eventsList.Clear();
  168. }
  169. }
  170. /// <summary>
  171. /// Gets the events that have been logged and clears the list of events.
  172. /// </summary>
  173. /// <returns>The events that have been logged</returns>
  174. /// <remarks>
  175. /// <para>
  176. /// Gets the events that have been logged and clears the list of events.
  177. /// </para>
  178. /// </remarks>
  179. virtual public LoggingEvent[] PopAllEvents()
  180. {
  181. lock (m_eventsList.SyncRoot)
  182. {
  183. LoggingEvent[] tmp = (LoggingEvent[]) m_eventsList.ToArray(typeof (LoggingEvent));
  184. m_eventsList.Clear();
  185. return tmp;
  186. }
  187. }
  188. #endregion Public Instance Methods
  189. #region Protected Instance Fields
  190. /// <summary>
  191. /// The list of events that have been appended.
  192. /// </summary>
  193. protected ArrayList m_eventsList;
  194. /// <summary>
  195. /// Value indicating which fields in the event should be fixed
  196. /// </summary>
  197. /// <remarks>
  198. /// By default all fields are fixed
  199. /// </remarks>
  200. protected FixFlags m_fixFlags = FixFlags.All;
  201. #endregion Protected Instance Fields
  202. }
  203. }