DatePatternConverter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.DateFormatter;
  22. using log4net.Core;
  23. namespace log4net.Util.PatternStringConverters
  24. {
  25. /// <summary>
  26. /// Write the current date to the output
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// Date pattern converter, uses a <see cref="IDateFormatter"/> to format
  31. /// the current date and time to the writer as a string.
  32. /// </para>
  33. /// <para>
  34. /// The value of the <see cref="log4net.Util.PatternConverter.Option"/> determines
  35. /// the formatting of the date. The following values are allowed:
  36. /// <list type="definition">
  37. /// <listheader>
  38. /// <term>Option value</term>
  39. /// <description>Output</description>
  40. /// </listheader>
  41. /// <item>
  42. /// <term>ISO8601</term>
  43. /// <description>
  44. /// Uses the <see cref="Iso8601DateFormatter"/> formatter.
  45. /// Formats using the <c>"yyyy-MM-dd HH:mm:ss,fff"</c> pattern.
  46. /// </description>
  47. /// </item>
  48. /// <item>
  49. /// <term>DATE</term>
  50. /// <description>
  51. /// Uses the <see cref="DateTimeDateFormatter"/> formatter.
  52. /// Formats using the <c>"dd MMM yyyy HH:mm:ss,fff"</c> for example, <c>"06 Nov 1994 15:49:37,459"</c>.
  53. /// </description>
  54. /// </item>
  55. /// <item>
  56. /// <term>ABSOLUTE</term>
  57. /// <description>
  58. /// Uses the <see cref="AbsoluteTimeDateFormatter"/> formatter.
  59. /// Formats using the <c>"HH:mm:ss,fff"</c> for example, <c>"15:49:37,459"</c>.
  60. /// </description>
  61. /// </item>
  62. /// <item>
  63. /// <term>other</term>
  64. /// <description>
  65. /// Any other pattern string uses the <see cref="SimpleDateFormatter"/> formatter.
  66. /// This formatter passes the pattern string to the <see cref="DateTime"/>
  67. /// <see cref="M:DateTime.ToString(string)"/> method.
  68. /// For details on valid patterns see
  69. /// <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationdatetimeformatinfoclasstopic.asp">DateTimeFormatInfo Class</a>.
  70. /// </description>
  71. /// </item>
  72. /// </list>
  73. /// </para>
  74. /// <para>
  75. /// The date and time is in the local time zone and is rendered in that zone.
  76. /// To output the time in Universal time see <see cref="UtcDatePatternConverter"/>.
  77. /// </para>
  78. /// </remarks>
  79. /// <author>Nicko Cadell</author>
  80. internal class DatePatternConverter : PatternConverter, IOptionHandler
  81. {
  82. /// <summary>
  83. /// The <see cref="IDateFormatter"/> used to render the date to a string
  84. /// </summary>
  85. /// <remarks>
  86. /// <para>
  87. /// The <see cref="IDateFormatter"/> used to render the date to a string
  88. /// </para>
  89. /// </remarks>
  90. protected IDateFormatter m_dateFormatter;
  91. #region Implementation of IOptionHandler
  92. /// <summary>
  93. /// Initialize the converter options
  94. /// </summary>
  95. /// <remarks>
  96. /// <para>
  97. /// This is part of the <see cref="IOptionHandler"/> delayed object
  98. /// activation scheme. The <see cref="ActivateOptions"/> method must
  99. /// be called on this object after the configuration properties have
  100. /// been set. Until <see cref="ActivateOptions"/> is called this
  101. /// object is in an undefined state and must not be used.
  102. /// </para>
  103. /// <para>
  104. /// If any of the configuration properties are modified then
  105. /// <see cref="ActivateOptions"/> must be called again.
  106. /// </para>
  107. /// </remarks>
  108. public void ActivateOptions()
  109. {
  110. string dateFormatStr = Option;
  111. if (dateFormatStr == null)
  112. {
  113. dateFormatStr = AbsoluteTimeDateFormatter.Iso8601TimeDateFormat;
  114. }
  115. if (SystemInfo.EqualsIgnoringCase(dateFormatStr, AbsoluteTimeDateFormatter.Iso8601TimeDateFormat))
  116. {
  117. m_dateFormatter = new Iso8601DateFormatter();
  118. }
  119. else if (SystemInfo.EqualsIgnoringCase(dateFormatStr, AbsoluteTimeDateFormatter.AbsoluteTimeDateFormat))
  120. {
  121. m_dateFormatter = new AbsoluteTimeDateFormatter();
  122. }
  123. else if (SystemInfo.EqualsIgnoringCase(dateFormatStr, AbsoluteTimeDateFormatter.DateAndTimeDateFormat))
  124. {
  125. m_dateFormatter = new DateTimeDateFormatter();
  126. }
  127. else
  128. {
  129. try
  130. {
  131. m_dateFormatter = new SimpleDateFormatter(dateFormatStr);
  132. }
  133. catch (Exception e)
  134. {
  135. LogLog.Error(declaringType, "Could not instantiate SimpleDateFormatter with ["+dateFormatStr+"]", e);
  136. m_dateFormatter = new Iso8601DateFormatter();
  137. }
  138. }
  139. }
  140. #endregion
  141. /// <summary>
  142. /// Write the current date to the output
  143. /// </summary>
  144. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  145. /// <param name="state">null, state is not set</param>
  146. /// <remarks>
  147. /// <para>
  148. /// Pass the current date and time to the <see cref="IDateFormatter"/>
  149. /// for it to render it to the writer.
  150. /// </para>
  151. /// <para>
  152. /// The date and time passed is in the local time zone.
  153. /// </para>
  154. /// </remarks>
  155. override protected void Convert(TextWriter writer, object state)
  156. {
  157. try
  158. {
  159. m_dateFormatter.FormatDate(DateTime.Now, writer);
  160. }
  161. catch (Exception ex)
  162. {
  163. LogLog.Error(declaringType, "Error occurred while converting date.", ex);
  164. }
  165. }
  166. #region Private Static Fields
  167. /// <summary>
  168. /// The fully qualified type of the DatePatternConverter class.
  169. /// </summary>
  170. /// <remarks>
  171. /// Used by the internal logger to record the Type of the
  172. /// log message.
  173. /// </remarks>
  174. private readonly static Type declaringType = typeof(DatePatternConverter);
  175. #endregion Private Static Fields
  176. }
  177. }