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