NamedPatternConverter.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Globalization;
  21. using System.Text;
  22. using System.IO;
  23. using log4net.Core;
  24. using log4net.Util;
  25. namespace log4net.Layout.Pattern
  26. {
  27. /// <summary>
  28. /// Converter to output and truncate <c>'.'</c> separated strings
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// This abstract class supports truncating a <c>'.'</c> separated string
  33. /// to show a specified number of elements from the right hand side.
  34. /// This is used to truncate class names that are fully qualified.
  35. /// </para>
  36. /// <para>
  37. /// Subclasses should override the <see cref="GetFullyQualifiedName"/> method to
  38. /// return the fully qualified string.
  39. /// </para>
  40. /// </remarks>
  41. /// <author>Nicko Cadell</author>
  42. public abstract class NamedPatternConverter : PatternLayoutConverter, IOptionHandler
  43. {
  44. private int m_precision = 0;
  45. #region Implementation of IOptionHandler
  46. /// <summary>
  47. /// Initialize the converter
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// This is part of the <see cref="IOptionHandler"/> delayed object
  52. /// activation scheme. The <see cref="ActivateOptions"/> method must
  53. /// be called on this object after the configuration properties have
  54. /// been set. Until <see cref="ActivateOptions"/> is called this
  55. /// object is in an undefined state and must not be used.
  56. /// </para>
  57. /// <para>
  58. /// If any of the configuration properties are modified then
  59. /// <see cref="ActivateOptions"/> must be called again.
  60. /// </para>
  61. /// </remarks>
  62. public void ActivateOptions()
  63. {
  64. m_precision = 0;
  65. if (Option != null)
  66. {
  67. string optStr = Option.Trim();
  68. if (optStr.Length > 0)
  69. {
  70. int precisionVal;
  71. if (SystemInfo.TryParse(optStr, out precisionVal))
  72. {
  73. if (precisionVal <= 0)
  74. {
  75. LogLog.Error(declaringType, "NamedPatternConverter: Precision option (" + optStr + ") isn't a positive integer.");
  76. }
  77. else
  78. {
  79. m_precision = precisionVal;
  80. }
  81. }
  82. else
  83. {
  84. LogLog.Error(declaringType, "NamedPatternConverter: Precision option \"" + optStr + "\" not a decimal integer.");
  85. }
  86. }
  87. }
  88. }
  89. #endregion
  90. /// <summary>
  91. /// Get the fully qualified string data
  92. /// </summary>
  93. /// <param name="loggingEvent">the event being logged</param>
  94. /// <returns>the fully qualified name</returns>
  95. /// <remarks>
  96. /// <para>
  97. /// Overridden by subclasses to get the fully qualified name before the
  98. /// precision is applied to it.
  99. /// </para>
  100. /// <para>
  101. /// Return the fully qualified <c>'.'</c> (dot/period) separated string.
  102. /// </para>
  103. /// </remarks>
  104. abstract protected string GetFullyQualifiedName(LoggingEvent loggingEvent);
  105. /// <summary>
  106. /// Convert the pattern to the rendered message
  107. /// </summary>
  108. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  109. /// <param name="loggingEvent">the event being logged</param>
  110. /// <remarks>
  111. /// Render the <see cref="GetFullyQualifiedName"/> to the precision
  112. /// specified by the <see cref="PatternConverter.Option"/> property.
  113. /// </remarks>
  114. sealed override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
  115. {
  116. string name = GetFullyQualifiedName(loggingEvent);
  117. if (m_precision <= 0 || name == null || name.Length < 2)
  118. {
  119. writer.Write(name);
  120. }
  121. else
  122. {
  123. int len = name.Length;
  124. string trailingDot = string.Empty;
  125. if (name.EndsWith(DOT))
  126. {
  127. trailingDot = DOT;
  128. name = name.Substring(0, len - 1);
  129. len--;
  130. }
  131. int end = name.LastIndexOf(DOT);
  132. for(int i = 1; end > 0 && i < m_precision; i++)
  133. {
  134. end = name.LastIndexOf('.', end - 1);
  135. }
  136. if (end == -1)
  137. {
  138. writer.Write(name + trailingDot);
  139. }
  140. else
  141. {
  142. writer.Write(name.Substring(end + 1, len - end - 1) + trailingDot);
  143. }
  144. }
  145. }
  146. #region Private Static Fields
  147. /// <summary>
  148. /// The fully qualified type of the NamedPatternConverter class.
  149. /// </summary>
  150. /// <remarks>
  151. /// Used by the internal logger to record the Type of the
  152. /// log message.
  153. /// </remarks>
  154. private readonly static Type declaringType = typeof(NamedPatternConverter);
  155. private const string DOT = ".";
  156. #endregion Private Static Fields
  157. }
  158. }