1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
20
using System.Globalization;
27
namespace log4net.Layout.Pattern
30
/// Converter to output and truncate <c>'.'</c> separated strings
34
/// This abstract class supports truncating a <c>'.'</c> separated string
35
/// to show a specified number of elements from the right hand side.
36
/// This is used to truncate class names that are fully qualified.
39
/// Subclasses should override the <see cref="GetFullyQualifiedName"/> method to
40
/// return the fully qualified string.
43
/// <author>Nicko Cadell</author>
44
internal abstract class NamedPatternConverter : PatternLayoutConverter, IOptionHandler
46
protected int m_precision = 0;
48
#region Implementation of IOptionHandler
51
/// Initialize the converter
55
/// This is part of the <see cref="IOptionHandler"/> delayed object
56
/// activation scheme. The <see cref="ActivateOptions"/> method must
57
/// be called on this object after the configuration properties have
58
/// been set. Until <see cref="ActivateOptions"/> is called this
59
/// object is in an undefined state and must not be used.
62
/// If any of the configuration properties are modified then
63
/// <see cref="ActivateOptions"/> must be called again.
66
public void ActivateOptions()
72
string optStr = Option.Trim();
73
if (optStr.Length > 0)
76
if (SystemInfo.TryParse(optStr, out precisionVal))
78
if (precisionVal <= 0)
80
LogLog.Error("NamedPatternConverter: Precision option (" + optStr + ") isn't a positive integer.");
84
m_precision = precisionVal;
89
LogLog.Error("NamedPatternConverter: Precision option \"" + optStr + "\" not a decimal integer.");
98
/// Get the fully qualified string data
100
/// <param name="loggingEvent">the event being logged</param>
101
/// <returns>the fully qualified name</returns>
104
/// Overridden by subclasses to get the fully qualified name before the
105
/// precision is applied to it.
108
/// Return the fully qualified <c>'.'</c> (dot/period) separated string.
111
abstract protected string GetFullyQualifiedName(LoggingEvent loggingEvent);
114
/// Convert the pattern to the rendered message
116
/// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
117
/// <param name="loggingEvent">the event being logged</param>
119
/// Render the <see cref="GetFullyQualifiedName"/> to the precision
120
/// specified by the <see cref="PatternConverter.Option"/> property.
122
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
124
string name = GetFullyQualifiedName(loggingEvent);
125
if (m_precision <= 0)
131
int len = name.Length;
133
// We subtract 1 from 'len' when assigning to 'end' to avoid out of
134
// bounds exception in return name.Substring(end+1, len). This can happen if
135
// precision is 1 and the logger name ends with a dot.
137
for(int i=m_precision; i>0; i--)
139
end = name.LastIndexOf('.', end-1);
146
writer.Write(name.Substring(end+1, len-end-1));