📄 patternstring.cs
字号:
#endregion
#region Static Constructor
/// <summary>
/// Initialize the global registry
/// </summary>
static PatternString()
{
s_globalRulesRegistry = new Hashtable(15);
s_globalRulesRegistry.Add("appdomain", typeof(AppDomainPatternConverter));
s_globalRulesRegistry.Add("date", typeof(DatePatternConverter));
#if !NETCF
s_globalRulesRegistry.Add("env", typeof(EnvironmentPatternConverter));
#endif
s_globalRulesRegistry.Add("identity", typeof(IdentityPatternConverter));
s_globalRulesRegistry.Add("literal", typeof(LiteralPatternConverter));
s_globalRulesRegistry.Add("newline", typeof(NewLinePatternConverter));
s_globalRulesRegistry.Add("processid", typeof(ProcessIdPatternConverter));
s_globalRulesRegistry.Add("property", typeof(PropertyPatternConverter));
s_globalRulesRegistry.Add("random", typeof(RandomStringPatternConverter));
s_globalRulesRegistry.Add("username", typeof(UserNamePatternConverter));
s_globalRulesRegistry.Add("utcdate", typeof(UtcDatePatternConverter));
s_globalRulesRegistry.Add("utcDate", typeof(UtcDatePatternConverter));
s_globalRulesRegistry.Add("UtcDate", typeof(UtcDatePatternConverter));
}
#endregion Static Constructor
#region Constructors
/// <summary>
/// Default constructor
/// </summary>
/// <remarks>
/// <para>
/// Initialize a new instance of <see cref="PatternString"/>
/// </para>
/// </remarks>
public PatternString()
{
}
/// <summary>
/// Constructs a PatternString
/// </summary>
/// <param name="pattern">The pattern to use with this PatternString</param>
/// <remarks>
/// <para>
/// Initialize a new instance of <see cref="PatternString"/> with the pattern specified.
/// </para>
/// </remarks>
public PatternString(string pattern)
{
m_pattern = pattern;
ActivateOptions();
}
#endregion
/// <summary>
/// Gets or sets the pattern formatting string
/// </summary>
/// <value>
/// The pattern formatting string
/// </value>
/// <remarks>
/// <para>
/// The <b>ConversionPattern</b> option. This is the string which
/// controls formatting and consists of a mix of literal content and
/// conversion specifiers.
/// </para>
/// </remarks>
public string ConversionPattern
{
get { return m_pattern; }
set { m_pattern = value; }
}
#region Implementation of IOptionHandler
/// <summary>
/// Initialize object options
/// </summary>
/// <remarks>
/// <para>
/// This is part of the <see cref="IOptionHandler"/> delayed object
/// activation scheme. The <see cref="ActivateOptions"/> method must
/// be called on this object after the configuration properties have
/// been set. Until <see cref="ActivateOptions"/> is called this
/// object is in an undefined state and must not be used.
/// </para>
/// <para>
/// If any of the configuration properties are modified then
/// <see cref="ActivateOptions"/> must be called again.
/// </para>
/// </remarks>
virtual public void ActivateOptions()
{
m_head = CreatePatternParser(m_pattern).Parse();
}
#endregion
/// <summary>
/// Create the <see cref="PatternParser"/> used to parse the pattern
/// </summary>
/// <param name="pattern">the pattern to parse</param>
/// <returns>The <see cref="PatternParser"/></returns>
/// <remarks>
/// <para>
/// Returns PatternParser used to parse the conversion string. Subclasses
/// may override this to return a subclass of PatternParser which recognize
/// custom conversion pattern name.
/// </para>
/// </remarks>
private PatternParser CreatePatternParser(string pattern)
{
PatternParser patternParser = new PatternParser(pattern);
// Add all the builtin patterns
foreach(DictionaryEntry entry in s_globalRulesRegistry)
{
patternParser.PatternConverters.Add(entry.Key, entry.Value);
}
// Add the instance patterns
foreach(DictionaryEntry entry in m_instanceRulesRegistry)
{
patternParser.PatternConverters[entry.Key] = entry.Value;
}
return patternParser;
}
/// <summary>
/// Produces a formatted string as specified by the conversion pattern.
/// </summary>
/// <param name="writer">The TextWriter to write the formatted event to</param>
/// <remarks>
/// <para>
/// Format the pattern to the <paramref name="writer"/>.
/// </para>
/// </remarks>
public void Format(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
PatternConverter c = m_head;
// loop through the chain of pattern converters
while(c != null)
{
c.Format(writer, null);
c = c.Next;
}
}
/// <summary>
/// Format the pattern as a string
/// </summary>
/// <returns>the pattern formatted as a string</returns>
/// <remarks>
/// <para>
/// Format the pattern to a string.
/// </para>
/// </remarks>
public string Format()
{
StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
Format(writer);
return writer.ToString();
}
/// <summary>
/// Add a converter to this PatternString
/// </summary>
/// <param name="converterInfo">the converter info</param>
/// <remarks>
/// <para>
/// This version of the method is used by the configurator.
/// Programmatic users should use the alternative <see cref="AddConverter(string,Type)"/> method.
/// </para>
/// </remarks>
public void AddConverter(ConverterInfo converterInfo)
{
AddConverter(converterInfo.Name, converterInfo.Type);
}
/// <summary>
/// Add a converter to this PatternString
/// </summary>
/// <param name="name">the name of the conversion pattern for this converter</param>
/// <param name="type">the type of the converter</param>
/// <remarks>
/// <para>
/// Add a converter to this PatternString
/// </para>
/// </remarks>
public void AddConverter(string name, Type type)
{
if (name == null) throw new ArgumentNullException("name");
if (type == null) throw new ArgumentNullException("type");
if (!typeof(PatternConverter).IsAssignableFrom(type))
{
throw new ArgumentException("The converter type specified ["+type+"] must be a subclass of log4net.Util.PatternConverter", "type");
}
m_instanceRulesRegistry[name] = type;
}
/// <summary>
/// Wrapper class used to map converter names to converter types
/// </summary>
/// <remarks>
/// <para>
/// Wrapper class used to map converter names to converter types
/// </para>
/// </remarks>
public sealed class ConverterInfo
{
private string m_name;
private Type m_type;
/// <summary>
/// default constructor
/// </summary>
public ConverterInfo()
{
}
/// <summary>
/// Gets or sets the name of the conversion pattern
/// </summary>
/// <value>
/// The name of the conversion pattern
/// </value>
/// <remarks>
/// <para>
/// Gets or sets the name of the conversion pattern
/// </para>
/// </remarks>
public string Name
{
get { return m_name; }
set { m_name = value; }
}
/// <summary>
/// Gets or sets the type of the converter
/// </summary>
/// <value>
/// The type of the converter
/// </value>
/// <remarks>
/// <para>
/// Gets or sets the type of the converter
/// </para>
/// </remarks>
public Type Type
{
get { return m_type; }
set { m_type = value; }
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -