DynamicPatternLayout.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 System.IO;
  22. using log4net.Core;
  23. using log4net.Layout.Pattern;
  24. using log4net.Util;
  25. namespace log4net.Layout
  26. {
  27. /// <summary>
  28. /// A flexible layout configurable with pattern string that re-evaluates on each call.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>This class is built on <see cref="PatternLayout"></see> and provides all the
  32. /// features and capabilities of PatternLayout. PatternLayout is a 'static' class
  33. /// in that its layout is done once at configuration time. This class will recreate
  34. /// the layout on each reference.</para>
  35. /// <para>One important difference between PatternLayout and DynamicPatternLayout is the
  36. /// treatment of the Header and Footer parameters in the configuration. The Header and Footer
  37. /// parameters for DynamicPatternLayout must be syntactically in the form of a PatternString,
  38. /// but should not be marked as type log4net.Util.PatternString. Doing so causes the
  39. /// pattern to be statically converted at configuration time and causes DynamicPatternLayout
  40. /// to perform the same as PatternLayout.</para>
  41. /// <para>Please see <see cref="PatternLayout"/> for complete documentation.</para>
  42. /// <example>
  43. /// &lt;layout type="log4net.Layout.DynamicPatternLayout"&gt;
  44. /// &lt;param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline" /&gt;
  45. /// &lt;param name="Footer" value="**** Trace Closed %date{yyyy-MM-dd HH:mm:ss.fff} ****%newline" /&gt;
  46. /// &lt;/layout&gt;
  47. /// </example>
  48. /// </remarks>
  49. public class DynamicPatternLayout: PatternLayout
  50. {
  51. #region Member Variables
  52. /// <summary>
  53. /// The header PatternString
  54. /// </summary>
  55. private PatternString m_headerPatternString = new PatternString("");
  56. /// <summary>
  57. /// The footer PatternString
  58. /// </summary>
  59. private PatternString m_footerPatternString = new PatternString("");
  60. #endregion
  61. #region Constructors
  62. /// <summary>
  63. /// Constructs a DynamicPatternLayout using the DefaultConversionPattern
  64. /// </summary>
  65. /// <remarks>
  66. /// <para>
  67. /// The default pattern just produces the application supplied message.
  68. /// </para>
  69. /// </remarks>
  70. public DynamicPatternLayout()
  71. : base()
  72. {
  73. }
  74. /// <summary>
  75. /// Constructs a DynamicPatternLayout using the supplied conversion pattern
  76. /// </summary>
  77. /// <param name="pattern">the pattern to use</param>
  78. /// <remarks>
  79. /// </remarks>
  80. public DynamicPatternLayout (string pattern)
  81. : base(pattern)
  82. {
  83. }
  84. #endregion
  85. #region Override implementation of LayoutSkeleton
  86. /// <summary>
  87. /// The header for the layout format.
  88. /// </summary>
  89. /// <value>the layout header</value>
  90. /// <remarks>
  91. /// <para>
  92. /// The Header text will be appended before any logging events
  93. /// are formatted and appended.
  94. /// </para>
  95. /// The pattern will be formatted on each get operation.
  96. /// </remarks>
  97. public override string Header
  98. {
  99. get
  100. {
  101. return m_headerPatternString.Format();
  102. }
  103. set
  104. {
  105. base.Header = value;
  106. m_headerPatternString = new PatternString(value);
  107. }
  108. } /* property DynamicPatternLayout Header */
  109. /// <summary>
  110. /// The footer for the layout format.
  111. /// </summary>
  112. /// <value>the layout footer</value>
  113. /// <remarks>
  114. /// <para>
  115. /// The Footer text will be appended after all the logging events
  116. /// have been formatted and appended.
  117. /// </para>
  118. /// The pattern will be formatted on each get operation.
  119. /// </remarks>
  120. public override string Footer
  121. {
  122. get
  123. {
  124. return m_footerPatternString.Format();
  125. }
  126. set
  127. {
  128. base.Footer = value;
  129. m_footerPatternString = new PatternString(value);
  130. }
  131. } /* property DynamicPatternLayout Footer */
  132. #endregion
  133. } /* class DynamicPatternLayout */
  134. } /* namespace log4net.Layout */