LiteralPatternConverter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Util;
  23. namespace log4net.Util.PatternStringConverters
  24. {
  25. /// <summary>
  26. /// Pattern converter for literal string instances in the pattern
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// Writes the literal string value specified in the
  31. /// <see cref="log4net.Util.PatternConverter.Option"/> property to
  32. /// the output.
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Nicko Cadell</author>
  36. internal class LiteralPatternConverter : PatternConverter
  37. {
  38. /// <summary>
  39. /// Set the next converter in the chain
  40. /// </summary>
  41. /// <param name="pc">The next pattern converter in the chain</param>
  42. /// <returns>The next pattern converter</returns>
  43. /// <remarks>
  44. /// <para>
  45. /// Special case the building of the pattern converter chain
  46. /// for <see cref="LiteralPatternConverter"/> instances. Two adjacent
  47. /// literals in the pattern can be represented by a single combined
  48. /// pattern converter. This implementation detects when a
  49. /// <see cref="LiteralPatternConverter"/> is added to the chain
  50. /// after this converter and combines its value with this converter's
  51. /// literal value.
  52. /// </para>
  53. /// </remarks>
  54. public override PatternConverter SetNext(PatternConverter pc)
  55. {
  56. LiteralPatternConverter literalPc = pc as LiteralPatternConverter;
  57. if (literalPc != null)
  58. {
  59. // Combine the two adjacent literals together
  60. Option = Option + literalPc.Option;
  61. // We are the next converter now
  62. return this;
  63. }
  64. return base.SetNext(pc);
  65. }
  66. /// <summary>
  67. /// Write the literal to the output
  68. /// </summary>
  69. /// <param name="writer">the writer to write to</param>
  70. /// <param name="state">null, not set</param>
  71. /// <remarks>
  72. /// <para>
  73. /// Override the formatting behavior to ignore the FormattingInfo
  74. /// because we have a literal instead.
  75. /// </para>
  76. /// <para>
  77. /// Writes the value of <see cref="log4net.Util.PatternConverter.Option"/>
  78. /// to the output <paramref name="writer"/>.
  79. /// </para>
  80. /// </remarks>
  81. override public void Format(TextWriter writer, object state)
  82. {
  83. writer.Write(Option);
  84. }
  85. /// <summary>
  86. /// Convert this pattern into the rendered message
  87. /// </summary>
  88. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  89. /// <param name="state">null, not set</param>
  90. /// <remarks>
  91. /// <para>
  92. /// This method is not used.
  93. /// </para>
  94. /// </remarks>
  95. override protected void Convert(TextWriter writer, object state)
  96. {
  97. throw new InvalidOperationException("Should never get here because of the overridden Format method");
  98. }
  99. }
  100. }