/Dependencies/log4net/Layout/XMLLayoutBase.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 259 lines · 71 code · 29 blank · 159 comment · 2 complexity · e749336f89662d5cf29a20e3c1e00168 MD5 · raw file

  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 System.Text;
  22. using System.Xml;
  23. using log4net.Util;
  24. using log4net.Core;
  25. namespace log4net.Layout
  26. {
  27. /// <summary>
  28. /// Layout that formats the log events as XML elements.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// This is an abstract class that must be subclassed by an implementation
  33. /// to conform to a specific schema.
  34. /// </para>
  35. /// <para>
  36. /// Deriving classes must implement the <see cref="FormatXml"/> method.
  37. /// </para>
  38. /// </remarks>
  39. /// <author>Nicko Cadell</author>
  40. /// <author>Gert Driesen</author>
  41. abstract public class XmlLayoutBase : LayoutSkeleton
  42. {
  43. #region Protected Instance Constructors
  44. /// <summary>
  45. /// Protected constructor to support subclasses
  46. /// </summary>
  47. /// <remarks>
  48. /// <para>
  49. /// Initializes a new instance of the <see cref="XmlLayoutBase" /> class
  50. /// with no location info.
  51. /// </para>
  52. /// </remarks>
  53. protected XmlLayoutBase() : this(false)
  54. {
  55. IgnoresException = false;
  56. }
  57. /// <summary>
  58. /// Protected constructor to support subclasses
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>
  62. /// The <paramref name="locationInfo" /> parameter determines whether
  63. /// location information will be output by the layout. If
  64. /// <paramref name="locationInfo" /> is set to <c>true</c>, then the
  65. /// file name and line number of the statement at the origin of the log
  66. /// statement will be output.
  67. /// </para>
  68. /// <para>
  69. /// If you are embedding this layout within an SMTPAppender
  70. /// then make sure to set the <b>LocationInfo</b> option of that
  71. /// appender as well.
  72. /// </para>
  73. /// </remarks>
  74. protected XmlLayoutBase(bool locationInfo)
  75. {
  76. IgnoresException = false;
  77. m_locationInfo = locationInfo;
  78. }
  79. #endregion Protected Instance Constructors
  80. #region Public Instance Properties
  81. /// <summary>
  82. /// Gets a value indicating whether to include location information in
  83. /// the XML events.
  84. /// </summary>
  85. /// <value>
  86. /// <c>true</c> if location information should be included in the XML
  87. /// events; otherwise, <c>false</c>.
  88. /// </value>
  89. /// <remarks>
  90. /// <para>
  91. /// If <see cref="LocationInfo" /> is set to <c>true</c>, then the file
  92. /// name and line number of the statement at the origin of the log
  93. /// statement will be output.
  94. /// </para>
  95. /// <para>
  96. /// If you are embedding this layout within an <c>SMTPAppender</c>
  97. /// then make sure to set the <b>LocationInfo</b> option of that
  98. /// appender as well.
  99. /// </para>
  100. /// </remarks>
  101. public bool LocationInfo
  102. {
  103. get { return m_locationInfo; }
  104. set { m_locationInfo = value; }
  105. }
  106. /// <summary>
  107. /// The string to replace characters that can not be expressed in XML with.
  108. /// <remarks>
  109. /// <para>
  110. /// Not all characters may be expressed in XML. This property contains the
  111. /// string to replace those that can not with. This defaults to a ?. Set it
  112. /// to the empty string to simply remove offending characters. For more
  113. /// details on the allowed character ranges see http://www.w3.org/TR/REC-xml/#charsets
  114. /// Character replacement will occur in the log message, the property names
  115. /// and the property values.
  116. /// </para>
  117. /// </remarks>
  118. /// </summary>
  119. public string InvalidCharReplacement
  120. {
  121. get {return m_invalidCharReplacement;}
  122. set {m_invalidCharReplacement=value;}
  123. }
  124. #endregion
  125. #region Implementation of IOptionHandler
  126. /// <summary>
  127. /// Initialize layout options
  128. /// </summary>
  129. /// <remarks>
  130. /// <para>
  131. /// This is part of the <see cref="IOptionHandler"/> delayed object
  132. /// activation scheme. The <see cref="ActivateOptions"/> method must
  133. /// be called on this object after the configuration properties have
  134. /// been set. Until <see cref="ActivateOptions"/> is called this
  135. /// object is in an undefined state and must not be used.
  136. /// </para>
  137. /// <para>
  138. /// If any of the configuration properties are modified then
  139. /// <see cref="ActivateOptions"/> must be called again.
  140. /// </para>
  141. /// </remarks>
  142. override public void ActivateOptions()
  143. {
  144. // nothing to do
  145. }
  146. #endregion Implementation of IOptionHandler
  147. #region Override implementation of LayoutSkeleton
  148. /// <summary>
  149. /// Gets the content type output by this layout.
  150. /// </summary>
  151. /// <value>
  152. /// As this is the XML layout, the value is always <c>"text/xml"</c>.
  153. /// </value>
  154. /// <remarks>
  155. /// <para>
  156. /// As this is the XML layout, the value is always <c>"text/xml"</c>.
  157. /// </para>
  158. /// </remarks>
  159. override public string ContentType
  160. {
  161. get { return "text/xml"; }
  162. }
  163. /// <summary>
  164. /// Produces a formatted string.
  165. /// </summary>
  166. /// <param name="loggingEvent">The event being logged.</param>
  167. /// <param name="writer">The TextWriter to write the formatted event to</param>
  168. /// <remarks>
  169. /// <para>
  170. /// Format the <see cref="LoggingEvent"/> and write it to the <see cref="TextWriter"/>.
  171. /// </para>
  172. /// <para>
  173. /// This method creates an <see cref="XmlTextWriter"/> that writes to the
  174. /// <paramref name="writer"/>. The <see cref="XmlTextWriter"/> is passed
  175. /// to the <see cref="FormatXml"/> method. Subclasses should override the
  176. /// <see cref="FormatXml"/> method rather than this method.
  177. /// </para>
  178. /// </remarks>
  179. override public void Format(TextWriter writer, LoggingEvent loggingEvent)
  180. {
  181. if (loggingEvent == null)
  182. {
  183. throw new ArgumentNullException("loggingEvent");
  184. }
  185. // Attach the protected writer to the TextWriter passed in
  186. m_protectCloseTextWriter.Attach(writer);
  187. XmlTextWriter xmlWriter = new XmlTextWriter(m_protectCloseTextWriter);
  188. xmlWriter.Formatting = Formatting.None;
  189. xmlWriter.Namespaces = false;
  190. // Write the event to the writer
  191. FormatXml(xmlWriter, loggingEvent);
  192. xmlWriter.WriteWhitespace(SystemInfo.NewLine);
  193. // Close on xmlWriter will ensure xml is flushed
  194. // the protected writer will ignore the actual close
  195. xmlWriter.Close();
  196. // detach from the writer
  197. m_protectCloseTextWriter.Attach(null);
  198. }
  199. #endregion Override implementation of LayoutSkeleton
  200. #region Protected Instance Methods
  201. /// <summary>
  202. /// Does the actual writing of the XML.
  203. /// </summary>
  204. /// <param name="writer">The writer to use to output the event to.</param>
  205. /// <param name="loggingEvent">The event to write.</param>
  206. /// <remarks>
  207. /// <para>
  208. /// Subclasses should override this method to format
  209. /// the <see cref="LoggingEvent"/> as XML.
  210. /// </para>
  211. /// </remarks>
  212. abstract protected void FormatXml(XmlWriter writer, LoggingEvent loggingEvent);
  213. #endregion Protected Instance Methods
  214. #region Private Instance Fields
  215. /// <summary>
  216. /// Flag to indicate if location information should be included in
  217. /// the XML events.
  218. /// </summary>
  219. private bool m_locationInfo = false;
  220. /// <summary>
  221. /// Writer adapter that ignores Close
  222. /// </summary>
  223. private readonly ProtectCloseTextWriter m_protectCloseTextWriter = new ProtectCloseTextWriter(null);
  224. /// <summary>
  225. /// The string to replace invalid chars with
  226. /// </summary>
  227. private string m_invalidCharReplacement="?";
  228. #endregion Private Instance Fields
  229. }
  230. }