PatternLayoutConverter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 System.Collections;
  23. using log4net.Core;
  24. using log4net.Util;
  25. using log4net.Repository;
  26. namespace log4net.Layout.Pattern
  27. {
  28. /// <summary>
  29. /// Abstract class that provides the formatting functionality that
  30. /// derived classes need.
  31. /// </summary>
  32. /// <remarks>
  33. /// Conversion specifiers in a conversion patterns are parsed to
  34. /// individual PatternConverters. Each of which is responsible for
  35. /// converting a logging event in a converter specific manner.
  36. /// </remarks>
  37. /// <author>Nicko Cadell</author>
  38. public abstract class PatternLayoutConverter : PatternConverter
  39. {
  40. #region Protected Instance Constructors
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="PatternLayoutConverter" /> class.
  43. /// </summary>
  44. protected PatternLayoutConverter()
  45. {
  46. }
  47. #endregion Protected Instance Constructors
  48. #region Public Properties
  49. /// <summary>
  50. /// Flag indicating if this converter handles the logging event exception
  51. /// </summary>
  52. /// <value><c>false</c> if this converter handles the logging event exception</value>
  53. /// <remarks>
  54. /// <para>
  55. /// If this converter handles the exception object contained within
  56. /// <see cref="LoggingEvent"/>, then this property should be set to
  57. /// <c>false</c>. Otherwise, if the layout ignores the exception
  58. /// object, then the property should be set to <c>true</c>.
  59. /// </para>
  60. /// <para>
  61. /// Set this value to override a this default setting. The default
  62. /// value is <c>true</c>, this converter does not handle the exception.
  63. /// </para>
  64. /// </remarks>
  65. virtual public bool IgnoresException
  66. {
  67. get { return m_ignoresException; }
  68. set { m_ignoresException = value; }
  69. }
  70. #endregion Public Properties
  71. #region Protected Abstract Methods
  72. /// <summary>
  73. /// Derived pattern converters must override this method in order to
  74. /// convert conversion specifiers in the correct way.
  75. /// </summary>
  76. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  77. /// <param name="loggingEvent">The <see cref="LoggingEvent" /> on which the pattern converter should be executed.</param>
  78. abstract protected void Convert(TextWriter writer, LoggingEvent loggingEvent);
  79. #endregion Protected Abstract Methods
  80. #region Protected Methods
  81. /// <summary>
  82. /// Derived pattern converters must override this method in order to
  83. /// convert conversion specifiers in the correct way.
  84. /// </summary>
  85. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  86. /// <param name="state">The state object on which the pattern converter should be executed.</param>
  87. override protected void Convert(TextWriter writer, object state)
  88. {
  89. LoggingEvent loggingEvent = state as LoggingEvent;
  90. if (loggingEvent != null)
  91. {
  92. Convert(writer, loggingEvent);
  93. }
  94. else
  95. {
  96. throw new ArgumentException("state must be of type ["+typeof(LoggingEvent).FullName+"]", "state");
  97. }
  98. }
  99. #endregion Protected Methods
  100. /// <summary>
  101. /// Flag indicating if this converter handles exceptions
  102. /// </summary>
  103. /// <remarks>
  104. /// <c>false</c> if this converter handles exceptions
  105. /// </remarks>
  106. private bool m_ignoresException = true;
  107. }
  108. }