-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
LoggerNameMatcher.cs
270 lines (255 loc) · 12.1 KB
/
LoggerNameMatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// Copyright (c) 2004-2019 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Jaroslaw Kowalski nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
namespace NLog.Config
{
using System;
using System.Text.RegularExpressions;
using NLog.Common;
/// <summary>
/// Encapsulates <see cref="LoggingRule.LoggerNamePattern"/> and the logic to match the actual logger name
/// All subclasses defines immutable objects.
/// Concrete subclasses defines various matching rules through <see cref="LoggerNameMatcher.NameMatches(string)"/>
/// </summary>
abstract class LoggerNameMatcher
{
/// <summary>
/// Creates a concrete <see cref="LoggerNameMatcher"/> based on <paramref name="loggerNamePattern"/>.
/// </summary>
/// <remarks>
/// Rules used to select the concrete implementation returned:
/// <list type="number">
/// <item>if <paramref name="loggerNamePattern"/> is null => returns <see cref="NoneLoggerNameMatcher"/> (never matches)</item>
/// <item>if <paramref name="loggerNamePattern"/> doesn't contains any '*' nor '?' => returns <see cref="EqualsLoggerNameMatcher"/> (matches only on case sensitive equals)</item>
/// <item>if <paramref name="loggerNamePattern"/> == '*' => returns <see cref="AllLoggerNameMatcher"/> (always matches)</item>
/// <item>if <paramref name="loggerNamePattern"/> doesn't contain '?'
/// <list type="number">
/// <item>if <paramref name="loggerNamePattern"/> contains exactly 2 '*' one at the beginning and one at the end (i.e. "*foobar*) => returns <see cref="ContainsLoggerNameMatcher"/></item>
/// <item>if <paramref name="loggerNamePattern"/> contains exactly 1 '*' at the beginning (i.e. "*foobar") => returns <see cref="EndsWithLoggerNameMatcher"/></item>
/// <item>if <paramref name="loggerNamePattern"/> contains exactly 1 '*' at the end (i.e. "foobar*") => returns <see cref="StartsWithLoggerNameMatcher"/></item>
/// </list>
/// </item>
/// <item>returns <see cref="MultiplePatternLoggerNameMatcher"/></item>
/// </list>
/// </remarks>
/// <param name="loggerNamePattern">
/// It may include one or more '*' or '?' wildcards at any position.
/// <list type="bullet">
/// <item>'*' means zero or more occurrecnces of any character</item>
/// <item>'?' means exactly one occurrence of any character</item>
/// </list>
/// </param>
/// <returns>A concrete <see cref="LoggerNameMatcher"/></returns>
public static LoggerNameMatcher Create(string loggerNamePattern)
{
if (loggerNamePattern == null)
return NoneLoggerNameMatcher.Instance;
int starPos1 = loggerNamePattern.IndexOf('*');
int starPos2 = loggerNamePattern.IndexOf('*', starPos1 + 1);
int questionPos = loggerNamePattern.IndexOf('?');
if (starPos1 < 0 && questionPos < 0)
return new EqualsLoggerNameMatcher(loggerNamePattern);
if (loggerNamePattern == "*")
return AllLoggerNameMatcher.Instance;
if (questionPos < 0)
{
if (starPos1 == 0 && starPos2 == loggerNamePattern.Length - 1)
return new ContainsLoggerNameMatcher(loggerNamePattern);
if (starPos2 < 0)
{
var loggerNameMatcher = CreateStartsOrEndsWithLoggerNameMatcher(loggerNamePattern, starPos1);
if (loggerNameMatcher != null)
{
return loggerNameMatcher;
}
}
}
return new MultiplePatternLoggerNameMatcher(loggerNamePattern);
}
private static LoggerNameMatcher CreateStartsOrEndsWithLoggerNameMatcher(string loggerNamePattern, int starPos1)
{
if (starPos1 == 0)
return new EndsWithLoggerNameMatcher(loggerNamePattern);
if (starPos1 == loggerNamePattern.Length - 1)
return new StartsWithLoggerNameMatcher(loggerNamePattern);
return null;
}
/// <summary>
/// Returns the argument passed to <see cref="LoggerNameMatcher.Create(string)"/>
/// </summary>
public string Pattern { get; }
protected readonly string _matchingArgument;
private readonly string _toString;
protected LoggerNameMatcher(string pattern, string matchingArgument)
{
Pattern = pattern;
_matchingArgument = matchingArgument;
_toString = "logNamePattern: (" + matchingArgument + ":" + Name + ")";
}
public override string ToString()
{
return _toString;
}
protected abstract string Name { get; }
/// <summary>
/// Checks whether given name matches the logger name pattern.
/// </summary>
/// <param name="loggerName">String to be matched.</param>
/// <returns>A value of <see langword="true"/> when the name matches, <see langword="false" /> otherwise.</returns>
public abstract bool NameMatches(string loggerName);
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that never matches.
/// Used when pattern is null
/// </summary>
class NoneLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "None";
public static readonly NoneLoggerNameMatcher Instance = new NoneLoggerNameMatcher();
private NoneLoggerNameMatcher()
: base(null, null)
{
}
public override bool NameMatches(string loggerName)
{
return false;
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that always matches.
/// Used when pattern is '*'
/// </summary>
class AllLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "All";
public static readonly AllLoggerNameMatcher Instance = new AllLoggerNameMatcher();
private AllLoggerNameMatcher()
: base("*", null) { }
public override bool NameMatches(string loggerName)
{
return true;
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that matches with a case-sensitive Equals
/// Used when pattern is a string without wildcards '?' '*'
/// </summary>
class EqualsLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "Equals";
public EqualsLoggerNameMatcher(string pattern)
: base(pattern, pattern) { }
public override bool NameMatches(string loggerName)
{
if (loggerName == null) return _matchingArgument == null;
return loggerName.Equals(_matchingArgument, StringComparison.Ordinal);
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that matches with a case-sensitive StartsWith
/// Used when pattern is a string like "*foobar"
/// </summary>
class StartsWithLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "StartsWith";
public StartsWithLoggerNameMatcher(string pattern)
: base(pattern, pattern.Substring(0, pattern.Length - 1)) { }
public override bool NameMatches(string loggerName)
{
if (loggerName == null) return _matchingArgument == null;
return loggerName.StartsWith(_matchingArgument, StringComparison.Ordinal);
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that matches with a case-sensitive EndsWith
/// Used when pattern is a string like "foobar*"
/// </summary>
class EndsWithLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "EndsWith";
public EndsWithLoggerNameMatcher(string pattern)
: base(pattern, pattern.Substring(1)) { }
public override bool NameMatches(string loggerName)
{
if (loggerName == null) return _matchingArgument == null;
return loggerName.EndsWith(_matchingArgument, StringComparison.Ordinal);
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that matches with a case-sensitive Contains
/// Used when pattern is a string like "*foobar*"
/// </summary>
class ContainsLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "Contains";
public ContainsLoggerNameMatcher(string pattern)
: base(pattern, pattern.Substring(1, pattern.Length - 2)) { }
public override bool NameMatches(string loggerName)
{
if (loggerName == null) return _matchingArgument == null;
return loggerName.IndexOf(_matchingArgument, StringComparison.Ordinal) >= 0;
}
}
/// <summary>
/// Defines a <see cref="LoggerNameMatcher"/> that matches with a complex wildcards combinations:
/// <list type="bullet">
/// <item>'*' means zero or more occurrences of any character</item>
/// <item>'?' means exactly one occurrence of any character</item>
/// </list>
/// used when pattern is a string containing any number of '?' or '*' in any position
/// i.e. "*Server[*].Connection[?]"
/// </summary>
class MultiplePatternLoggerNameMatcher : LoggerNameMatcher
{
protected override string Name => "MultiplePattern";
private readonly Regex _regex;
private static string ConvertToRegex(string wildcardsPattern)
{
return
'^' +
Regex.Escape(wildcardsPattern)
.Replace("\\*", ".*")
.Replace("\\?", ".")
+ '$';
}
public MultiplePatternLoggerNameMatcher(string pattern)
: base(pattern, ConvertToRegex(pattern))
{
_regex = new Regex(_matchingArgument, RegexOptions.CultureInvariant);
}
public override bool NameMatches(string loggerName)
{
if (loggerName == null) return false;
return _regex.IsMatch(loggerName);
}
}
}
}