UtcDatePatternConverter.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.Util.PatternStringConverters
  26. {
  27. /// <summary>
  28. /// Write the UTC date time to the output
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// Date pattern converter, uses a <see cref="IDateFormatter"/> to format
  33. /// the current date and time in Universal time.
  34. /// </para>
  35. /// <para>
  36. /// See the <see cref="DatePatternConverter"/> for details on the date pattern syntax.
  37. /// </para>
  38. /// </remarks>
  39. /// <seealso cref="DatePatternConverter"/>
  40. /// <author>Nicko Cadell</author>
  41. internal class UtcDatePatternConverter : DatePatternConverter
  42. {
  43. /// <summary>
  44. /// Write the current date and time to the output
  45. /// </summary>
  46. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  47. /// <param name="state">null, state is not set</param>
  48. /// <remarks>
  49. /// <para>
  50. /// Pass the current date and time to the <see cref="IDateFormatter"/>
  51. /// for it to render it to the writer.
  52. /// </para>
  53. /// <para>
  54. /// The date is in Universal time when it is rendered.
  55. /// </para>
  56. /// </remarks>
  57. /// <seealso cref="DatePatternConverter"/>
  58. override protected void Convert(TextWriter writer, object state)
  59. {
  60. try
  61. {
  62. m_dateFormatter.FormatDate(DateTime.UtcNow, writer);
  63. }
  64. catch (Exception ex)
  65. {
  66. LogLog.Error(declaringType, "Error occurred while converting date.", ex);
  67. }
  68. }
  69. #region Private Static Fields
  70. /// <summary>
  71. /// The fully qualified type of the UtcDatePatternConverter class.
  72. /// </summary>
  73. /// <remarks>
  74. /// Used by the internal logger to record the Type of the
  75. /// log message.
  76. /// </remarks>
  77. private readonly static Type declaringType = typeof(UtcDatePatternConverter);
  78. #endregion Private Static Fields
  79. }
  80. }