RandomStringPatternConverter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using log4net.DateFormatter;
  24. using log4net.Core;
  25. namespace log4net.Util.PatternStringConverters
  26. {
  27. /// <summary>
  28. /// A Pattern converter that generates a string of random characters
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// The converter generates a string of random characters. By default
  33. /// the string is length 4. This can be changed by setting the <see cref="PatternConverter.Option"/>
  34. /// to the string value of the length required.
  35. /// </para>
  36. /// <para>
  37. /// The random characters in the string are limited to uppercase letters
  38. /// and numbers only.
  39. /// </para>
  40. /// <para>
  41. /// The random number generator used by this class is not cryptographically secure.
  42. /// </para>
  43. /// </remarks>
  44. /// <author>Nicko Cadell</author>
  45. internal sealed class RandomStringPatternConverter : PatternConverter, IOptionHandler
  46. {
  47. /// <summary>
  48. /// Shared random number generator
  49. /// </summary>
  50. private static readonly Random s_random = new Random();
  51. /// <summary>
  52. /// Length of random string to generate. Default length 4.
  53. /// </summary>
  54. private int m_length = 4;
  55. #region Implementation of IOptionHandler
  56. /// <summary>
  57. /// Initialize the converter options
  58. /// </summary>
  59. /// <remarks>
  60. /// <para>
  61. /// This is part of the <see cref="IOptionHandler"/> delayed object
  62. /// activation scheme. The <see cref="ActivateOptions"/> method must
  63. /// be called on this object after the configuration properties have
  64. /// been set. Until <see cref="ActivateOptions"/> is called this
  65. /// object is in an undefined state and must not be used.
  66. /// </para>
  67. /// <para>
  68. /// If any of the configuration properties are modified then
  69. /// <see cref="ActivateOptions"/> must be called again.
  70. /// </para>
  71. /// </remarks>
  72. public void ActivateOptions()
  73. {
  74. string optionStr = Option;
  75. if (optionStr != null && optionStr.Length > 0)
  76. {
  77. int lengthVal;
  78. if (SystemInfo.TryParse(optionStr, out lengthVal))
  79. {
  80. m_length = lengthVal;
  81. }
  82. else
  83. {
  84. LogLog.Error(declaringType, "RandomStringPatternConverter: Could not convert Option ["+optionStr+"] to Length Int32");
  85. }
  86. }
  87. }
  88. #endregion
  89. /// <summary>
  90. /// Write a randoim string to the output
  91. /// </summary>
  92. /// <param name="writer">the writer to write to</param>
  93. /// <param name="state">null, state is not set</param>
  94. /// <remarks>
  95. /// <para>
  96. /// Write a randoim string to the output <paramref name="writer"/>.
  97. /// </para>
  98. /// </remarks>
  99. override protected void Convert(TextWriter writer, object state)
  100. {
  101. try
  102. {
  103. lock(s_random)
  104. {
  105. for(int i=0; i<m_length; i++)
  106. {
  107. int randValue = s_random.Next(36);
  108. if (randValue < 26)
  109. {
  110. // Letter
  111. char ch = (char)('A' + randValue);
  112. writer.Write(ch);
  113. }
  114. else if (randValue < 36)
  115. {
  116. // Number
  117. char ch = (char)('0' + (randValue - 26));
  118. writer.Write(ch);
  119. }
  120. else
  121. {
  122. // Should not get here
  123. writer.Write('X');
  124. }
  125. }
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. LogLog.Error(declaringType, "Error occurred while converting.", ex);
  131. }
  132. }
  133. #region Private Static Fields
  134. /// <summary>
  135. /// The fully qualified type of the RandomStringPatternConverter class.
  136. /// </summary>
  137. /// <remarks>
  138. /// Used by the internal logger to record the Type of the
  139. /// log message.
  140. /// </remarks>
  141. private readonly static Type declaringType = typeof(RandomStringPatternConverter);
  142. #endregion Private Static Fields
  143. }
  144. }