LayoutSkeleton.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.IO;
  21. using log4net;
  22. using log4net.Core;
  23. namespace log4net.Layout
  24. {
  25. /// <summary>
  26. /// Extend this abstract class to create your own log layout format.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// This is the base implementation of the <see cref="ILayout"/>
  31. /// interface. Most layout objects should extend this class.
  32. /// </para>
  33. /// </remarks>
  34. /// <remarks>
  35. /// <note type="inheritinfo">
  36. /// <para>
  37. /// Subclasses must implement the <see cref="M:Format(TextWriter,LoggingEvent)"/>
  38. /// method.
  39. /// </para>
  40. /// <para>
  41. /// Subclasses should set the <see cref="IgnoresException"/> in their default
  42. /// constructor.
  43. /// </para>
  44. /// </note>
  45. /// </remarks>
  46. /// <author>Nicko Cadell</author>
  47. /// <author>Gert Driesen</author>
  48. public abstract class LayoutSkeleton : ILayout, IOptionHandler
  49. {
  50. #region Member Variables
  51. /// <summary>
  52. /// The header text
  53. /// </summary>
  54. /// <remarks>
  55. /// <para>
  56. /// See <see cref="Header"/> for more information.
  57. /// </para>
  58. /// </remarks>
  59. private string m_header = null;
  60. /// <summary>
  61. /// The footer text
  62. /// </summary>
  63. /// <remarks>
  64. /// <para>
  65. /// See <see cref="Footer"/> for more information.
  66. /// </para>
  67. /// </remarks>
  68. private string m_footer = null;
  69. /// <summary>
  70. /// Flag indicating if this layout handles exceptions
  71. /// </summary>
  72. /// <remarks>
  73. /// <para>
  74. /// <c>false</c> if this layout handles exceptions
  75. /// </para>
  76. /// </remarks>
  77. private bool m_ignoresException = true;
  78. #endregion
  79. #region Constructors
  80. /// <summary>
  81. /// Empty default constructor
  82. /// </summary>
  83. /// <remarks>
  84. /// <para>
  85. /// Empty default constructor
  86. /// </para>
  87. /// </remarks>
  88. protected LayoutSkeleton()
  89. {
  90. }
  91. #endregion
  92. #region Implementation of IOptionHandler
  93. /// <summary>
  94. /// Activate component options
  95. /// </summary>
  96. /// <remarks>
  97. /// <para>
  98. /// This is part of the <see cref="IOptionHandler"/> delayed object
  99. /// activation scheme. The <see cref="ActivateOptions"/> method must
  100. /// be called on this object after the configuration properties have
  101. /// been set. Until <see cref="ActivateOptions"/> is called this
  102. /// object is in an undefined state and must not be used.
  103. /// </para>
  104. /// <para>
  105. /// If any of the configuration properties are modified then
  106. /// <see cref="ActivateOptions"/> must be called again.
  107. /// </para>
  108. /// <para>
  109. /// This method must be implemented by the subclass.
  110. /// </para>
  111. /// </remarks>
  112. abstract public void ActivateOptions();
  113. #endregion
  114. #region Implementation of ILayout
  115. /// <summary>
  116. /// Implement this method to create your own layout format.
  117. /// </summary>
  118. /// <param name="writer">The TextWriter to write the formatted event to</param>
  119. /// <param name="loggingEvent">The event to format</param>
  120. /// <remarks>
  121. /// <para>
  122. /// This method is called by an appender to format
  123. /// the <paramref name="loggingEvent"/> as text.
  124. /// </para>
  125. /// </remarks>
  126. abstract public void Format(TextWriter writer, LoggingEvent loggingEvent);
  127. /// <summary>
  128. /// Convenience method for easily formatting the logging event into a string variable.
  129. /// </summary>
  130. /// <param name="loggingEvent"></param>
  131. /// <remarks>
  132. /// Creates a new StringWriter instance to store the formatted logging event.
  133. /// </remarks>
  134. public string Format(LoggingEvent loggingEvent)
  135. {
  136. StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
  137. Format(writer, loggingEvent);
  138. return writer.ToString();
  139. }
  140. /// <summary>
  141. /// The content type output by this layout.
  142. /// </summary>
  143. /// <value>The content type is <c>"text/plain"</c></value>
  144. /// <remarks>
  145. /// <para>
  146. /// The content type output by this layout.
  147. /// </para>
  148. /// <para>
  149. /// This base class uses the value <c>"text/plain"</c>.
  150. /// To change this value a subclass must override this
  151. /// property.
  152. /// </para>
  153. /// </remarks>
  154. virtual public string ContentType
  155. {
  156. get { return "text/plain"; }
  157. }
  158. /// <summary>
  159. /// The header for the layout format.
  160. /// </summary>
  161. /// <value>the layout header</value>
  162. /// <remarks>
  163. /// <para>
  164. /// The Header text will be appended before any logging events
  165. /// are formatted and appended.
  166. /// </para>
  167. /// </remarks>
  168. virtual public string Header
  169. {
  170. get { return m_header; }
  171. set { m_header = value; }
  172. }
  173. /// <summary>
  174. /// The footer for the layout format.
  175. /// </summary>
  176. /// <value>the layout footer</value>
  177. /// <remarks>
  178. /// <para>
  179. /// The Footer text will be appended after all the logging events
  180. /// have been formatted and appended.
  181. /// </para>
  182. /// </remarks>
  183. virtual public string Footer
  184. {
  185. get { return m_footer; }
  186. set { m_footer = value; }
  187. }
  188. /// <summary>
  189. /// Flag indicating if this layout handles exceptions
  190. /// </summary>
  191. /// <value><c>false</c> if this layout handles exceptions</value>
  192. /// <remarks>
  193. /// <para>
  194. /// If this layout handles the exception object contained within
  195. /// <see cref="LoggingEvent"/>, then the layout should return
  196. /// <c>false</c>. Otherwise, if the layout ignores the exception
  197. /// object, then the layout should return <c>true</c>.
  198. /// </para>
  199. /// <para>
  200. /// Set this value to override a this default setting. The default
  201. /// value is <c>true</c>, this layout does not handle the exception.
  202. /// </para>
  203. /// </remarks>
  204. virtual public bool IgnoresException
  205. {
  206. get { return m_ignoresException; }
  207. set { m_ignoresException = value; }
  208. }
  209. #endregion
  210. }
  211. }