123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #region Apache License
- #endregion
- using System;
- using System.Text;
- using System.IO;
- using log4net.Util;
- using log4net.DateFormatter;
- using log4net.Core;
- namespace log4net.Util.PatternStringConverters
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- internal sealed class RandomStringPatternConverter : PatternConverter, IOptionHandler
- {
-
-
-
- private static readonly Random s_random = new Random();
-
-
-
- private int m_length = 4;
- #region Implementation of IOptionHandler
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void ActivateOptions()
- {
- string optionStr = Option;
- if (optionStr != null && optionStr.Length > 0)
- {
- int lengthVal;
- if (SystemInfo.TryParse(optionStr, out lengthVal))
- {
- m_length = lengthVal;
- }
- else
- {
- LogLog.Error(declaringType, "RandomStringPatternConverter: Could not convert Option ["+optionStr+"] to Length Int32");
- }
- }
- }
- #endregion
-
-
-
-
-
-
-
-
-
-
- override protected void Convert(TextWriter writer, object state)
- {
- try
- {
- lock(s_random)
- {
- for(int i=0; i<m_length; i++)
- {
- int randValue = s_random.Next(36);
- if (randValue < 26)
- {
-
- char ch = (char)('A' + randValue);
- writer.Write(ch);
- }
- else if (randValue < 36)
- {
-
- char ch = (char)('0' + (randValue - 26));
- writer.Write(ch);
- }
- else
- {
-
- writer.Write('X');
- }
- }
- }
- }
- catch (Exception ex)
- {
- LogLog.Error(declaringType, "Error occurred while converting.", ex);
- }
- }
- #region Private Static Fields
-
-
-
-
-
-
-
- private readonly static Type declaringType = typeof(RandomStringPatternConverter);
- #endregion Private Static Fields
- }
- }
|