#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Text;
using System.IO;
using log4net.Util;
using log4net.DateFormatter;
using log4net.Core;
namespace log4net.Util.PatternStringConverters
{
///
/// A Pattern converter that generates a string of random characters
///
///
///
/// The converter generates a string of random characters. By default
/// the string is length 4. This can be changed by setting the
/// to the string value of the length required.
///
///
/// The random characters in the string are limited to uppercase letters
/// and numbers only.
///
///
/// The random number generator used by this class is not cryptographically secure.
///
///
/// Nicko Cadell
internal sealed class RandomStringPatternConverter : PatternConverter, IOptionHandler
{
///
/// Shared random number generator
///
private static readonly Random s_random = new Random();
///
/// Length of random string to generate. Default length 4.
///
private int m_length = 4;
#region Implementation of IOptionHandler
///
/// Initialize the converter options
///
///
///
/// This is part of the delayed object
/// activation scheme. The method must
/// be called on this object after the configuration properties have
/// been set. Until is called this
/// object is in an undefined state and must not be used.
///
///
/// If any of the configuration properties are modified then
/// must be called again.
///
///
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
///
/// Write a randoim string to the output
///
/// the writer to write to
/// null, state is not set
///
///
/// Write a randoim string to the output .
///
///
protected override void Convert(TextWriter writer, object state)
{
try
{
lock(s_random)
{
for(int i=0; i
/// The fully qualified type of the RandomStringPatternConverter class.
///
///
/// Used by the internal logger to record the Type of the
/// log message.
///
private static readonly Type declaringType = typeof(RandomStringPatternConverter);
#endregion Private Static Fields
}
}