XmlLayoutBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #if NETSTANDARD1_3
  186. var settings = new XmlWriterSettings
  187. {
  188. Indent = false,
  189. OmitXmlDeclaration = true
  190. };
  191. var xmlWriter = XmlWriter.Create(new ProtectCloseTextWriter(writer), settings);
  192. #else
  193. XmlTextWriter xmlWriter = new XmlTextWriter(new ProtectCloseTextWriter(writer));
  194. xmlWriter.Formatting = Formatting.None;
  195. xmlWriter.Namespaces = false;
  196. #endif
  197. // Write the event to the writer
  198. FormatXml(xmlWriter, loggingEvent);
  199. xmlWriter.WriteWhitespace(SystemInfo.NewLine);
  200. // Close on xmlWriter will ensure xml is flushed
  201. // the protected writer will ignore the actual close
  202. xmlWriter.Close();
  203. }
  204. #endregion Override implementation of LayoutSkeleton
  205. #region Protected Instance Methods
  206. /// <summary>
  207. /// Does the actual writing of the XML.
  208. /// </summary>
  209. /// <param name="writer">The writer to use to output the event to.</param>
  210. /// <param name="loggingEvent">The event to write.</param>
  211. /// <remarks>
  212. /// <para>
  213. /// Subclasses should override this method to format
  214. /// the <see cref="LoggingEvent"/> as XML.
  215. /// </para>
  216. /// </remarks>
  217. abstract protected void FormatXml(XmlWriter writer, LoggingEvent loggingEvent);
  218. #endregion Protected Instance Methods
  219. #region Private Instance Fields
  220. /// <summary>
  221. /// Flag to indicate if location information should be included in
  222. /// the XML events.
  223. /// </summary>
  224. private bool m_locationInfo = false;
  225. /// <summary>
  226. /// The string to replace invalid chars with
  227. /// </summary>
  228. private string m_invalidCharReplacement="?";
  229. #endregion Private Instance Fields
  230. }
  231. }