CountingQuietTextWriter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.IO;
  21. using log4net.Core;
  22. namespace log4net.Util
  23. {
  24. /// <summary>
  25. /// Subclass of <see cref="QuietTextWriter"/> that maintains a count of
  26. /// the number of bytes written.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// This writer counts the number of bytes written.
  31. /// </para>
  32. /// </remarks>
  33. /// <author>Nicko Cadell</author>
  34. /// <author>Gert Driesen</author>
  35. public class CountingQuietTextWriter : QuietTextWriter
  36. {
  37. #region Public Instance Constructors
  38. /// <summary>
  39. /// Constructor
  40. /// </summary>
  41. /// <param name="writer">The <see cref="TextWriter" /> to actually write to.</param>
  42. /// <param name="errorHandler">The <see cref="IErrorHandler" /> to report errors to.</param>
  43. /// <remarks>
  44. /// <para>
  45. /// Creates a new instance of the <see cref="CountingQuietTextWriter" /> class
  46. /// with the specified <see cref="TextWriter" /> and <see cref="IErrorHandler" />.
  47. /// </para>
  48. /// </remarks>
  49. public CountingQuietTextWriter(TextWriter writer, IErrorHandler errorHandler) : base(writer, errorHandler)
  50. {
  51. m_countBytes = 0;
  52. }
  53. #endregion Public Instance Constructors
  54. #region Override implementation of QuietTextWriter
  55. /// <summary>
  56. /// Writes a character to the underlying writer and counts the number of bytes written.
  57. /// </summary>
  58. /// <param name="value">the char to write</param>
  59. /// <remarks>
  60. /// <para>
  61. /// Overrides implementation of <see cref="QuietTextWriter"/>. Counts
  62. /// the number of bytes written.
  63. /// </para>
  64. /// </remarks>
  65. public override void Write(char value)
  66. {
  67. try
  68. {
  69. base.Write(value);
  70. // get the number of bytes needed to represent the
  71. // char using the supplied encoding.
  72. m_countBytes += this.Encoding.GetByteCount(new char[] { value });
  73. }
  74. catch(Exception e)
  75. {
  76. this.ErrorHandler.Error("Failed to write [" + value + "].", e, ErrorCode.WriteFailure);
  77. }
  78. }
  79. /// <summary>
  80. /// Writes a buffer to the underlying writer and counts the number of bytes written.
  81. /// </summary>
  82. /// <param name="buffer">the buffer to write</param>
  83. /// <param name="index">the start index to write from</param>
  84. /// <param name="count">the number of characters to write</param>
  85. /// <remarks>
  86. /// <para>
  87. /// Overrides implementation of <see cref="QuietTextWriter"/>. Counts
  88. /// the number of bytes written.
  89. /// </para>
  90. /// </remarks>
  91. public override void Write(char[] buffer, int index, int count)
  92. {
  93. if (count > 0)
  94. {
  95. try
  96. {
  97. base.Write(buffer, index, count);
  98. // get the number of bytes needed to represent the
  99. // char array using the supplied encoding.
  100. m_countBytes += this.Encoding.GetByteCount(buffer, index, count);
  101. }
  102. catch(Exception e)
  103. {
  104. this.ErrorHandler.Error("Failed to write buffer.", e, ErrorCode.WriteFailure);
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Writes a string to the output and counts the number of bytes written.
  110. /// </summary>
  111. /// <param name="str">The string data to write to the output.</param>
  112. /// <remarks>
  113. /// <para>
  114. /// Overrides implementation of <see cref="QuietTextWriter"/>. Counts
  115. /// the number of bytes written.
  116. /// </para>
  117. /// </remarks>
  118. override public void Write(string str)
  119. {
  120. if (str != null && str.Length > 0)
  121. {
  122. try
  123. {
  124. base.Write(str);
  125. // get the number of bytes needed to represent the
  126. // string using the supplied encoding.
  127. m_countBytes += this.Encoding.GetByteCount(str);
  128. }
  129. catch(Exception e)
  130. {
  131. this.ErrorHandler.Error("Failed to write [" + str + "].", e, ErrorCode.WriteFailure);
  132. }
  133. }
  134. }
  135. #endregion Override implementation of QuietTextWriter
  136. #region Public Instance Properties
  137. /// <summary>
  138. /// Gets or sets the total number of bytes written.
  139. /// </summary>
  140. /// <value>
  141. /// The total number of bytes written.
  142. /// </value>
  143. /// <remarks>
  144. /// <para>
  145. /// Gets or sets the total number of bytes written.
  146. /// </para>
  147. /// </remarks>
  148. public long Count
  149. {
  150. get { return m_countBytes; }
  151. set { m_countBytes = value; }
  152. }
  153. #endregion Public Instance Properties
  154. #region Private Instance Fields
  155. /// <summary>
  156. /// Total number of bytes written.
  157. /// </summary>
  158. private long m_countBytes;
  159. #endregion Private Instance Fields
  160. }
  161. }