UtcDatePatternConverter.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Text;
  21. using System.IO;
  22. using log4net.Core;
  23. using log4net.Util;
  24. using log4net.DateFormatter;
  25. namespace log4net.Layout.Pattern
  26. {
  27. /// <summary>
  28. /// Write the TimeStamp to the output
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// Date pattern converter, uses a <see cref="IDateFormatter"/> to format
  33. /// the date of a <see cref="LoggingEvent"/>.
  34. /// </para>
  35. /// <para>
  36. /// Uses a <see cref="IDateFormatter"/> to format the <see cref="LoggingEvent.TimeStamp"/>
  37. /// in Universal time.
  38. /// </para>
  39. /// <para>
  40. /// See the <see cref="DatePatternConverter"/> for details on the date pattern syntax.
  41. /// </para>
  42. /// </remarks>
  43. /// <seealso cref="DatePatternConverter"/>
  44. /// <author>Nicko Cadell</author>
  45. internal class UtcDatePatternConverter : DatePatternConverter
  46. {
  47. /// <summary>
  48. /// Write the TimeStamp to the output
  49. /// </summary>
  50. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  51. /// <param name="loggingEvent">the event being logged</param>
  52. /// <remarks>
  53. /// <para>
  54. /// Pass the <see cref="LoggingEvent.TimeStamp"/> to the <see cref="IDateFormatter"/>
  55. /// for it to render it to the writer.
  56. /// </para>
  57. /// <para>
  58. /// The <see cref="LoggingEvent.TimeStamp"/> passed is in the local time zone, this is converted
  59. /// to Universal time before it is rendered.
  60. /// </para>
  61. /// </remarks>
  62. /// <seealso cref="DatePatternConverter"/>
  63. override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
  64. {
  65. try
  66. {
  67. m_dateFormatter.FormatDate(loggingEvent.TimeStampUtc, writer);
  68. }
  69. catch (Exception ex)
  70. {
  71. LogLog.Error(declaringType, "Error occurred while converting date.", ex);
  72. }
  73. }
  74. #region Private Static Fields
  75. /// <summary>
  76. /// The fully qualified type of the UtcDatePatternConverter class.
  77. /// </summary>
  78. /// <remarks>
  79. /// Used by the internal logger to record the Type of the
  80. /// log message.
  81. /// </remarks>
  82. private readonly static Type declaringType = typeof(UtcDatePatternConverter);
  83. #endregion Private Static Fields
  84. }
  85. }