Skip to content

Commit 1e94fed

Browse files
committed
Fixed several issues with resolving log levels based on settings order and null/empty sources
1 parent 09e6351 commit 1e94fed

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/Exceptionless/Extensions/StringExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ public static bool AnyWildcardMatches(this string value, IEnumerable<string> pat
3333
}
3434

3535
public static bool IsPatternMatch(this string value, string pattern, bool ignoreCase = true) {
36-
if (pattern == null || value == null)
37-
return false;
36+
if (pattern == null)
37+
return value == null;
3838

3939
if (pattern.Equals("*"))
4040
return true;
4141

42+
if (value == null)
43+
return false;
44+
4245
bool startsWithWildcard = pattern.StartsWith("*");
4346
if (startsWithWildcard)
4447
pattern = pattern.Substring(1);

src/Exceptionless/Models/Collections/SettingsDictionary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected override void OnChanged(ChangedEventArgs<KeyValuePair<string, string>>
194194
private readonly ConcurrentDictionary<string, LogLevel> _minLogLevels = new ConcurrentDictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase);
195195

196196
public LogLevel GetMinLogLevel(string loggerName) {
197-
if (String.IsNullOrEmpty(loggerName))
197+
if (loggerName == null)
198198
loggerName = "*";
199199

200200
LogLevel minLogLevel;
@@ -237,7 +237,6 @@ private string GetTypeAndSourceSetting(string type, string source, string defaul
237237
ConcurrentDictionary<string, bool> sourceDictionary;
238238
string sourcePrefix;
239239
if (!_typeSourceEnabled.TryGetValue(type, out sourceDictionary)) {
240-
241240
sourceDictionary = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
242241
_typeSourceEnabled.TryAdd(type, sourceDictionary);
243242
sourcePrefix = "@@" + type + ":";
@@ -256,6 +255,7 @@ private string GetTypeAndSourceSetting(string type, string source, string defaul
256255
var sourceSettings = this
257256
.Where(kvp => kvp.Key.StartsWith(sourcePrefix))
258257
.Select(kvp => new KeyValuePair<string, string>(kvp.Key.Substring(sourcePrefix.Length), kvp.Value))
258+
.OrderByDescending(s => s.Key.Length).ThenByDescending(s => s.Key) // sort by most qualified and ensure * comes after a-z.
259259
.ToList();
260260

261261
foreach (var kvp in sourceSettings) {

test/Exceptionless.Tests/Exceptionless.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<ItemGroup>
3636
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
3737
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
38-
<PackageReference Include="Moq" Version="4.14.6" />
38+
<PackageReference Include="Moq" Version="4.14.7" />
3939
<PackageReference Include="xunit" Version="2.4.1" />
4040
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
4141
<PrivateAssets>all</PrivateAssets>

test/Exceptionless.Tests/Plugins/PluginTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ public void EventExclusionPlugin_EventExclusions() {
152152
[InlineData("Test", "Trace", null, null, false)]
153153
[InlineData("Test", "Off", null, null, true)]
154154
[InlineData("Test", "Abc", null, null, false)]
155+
[InlineData(null, "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix, "Off", false)]
156+
[InlineData(null, "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "*", "Off", true)]
157+
[InlineData("", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix, "Off", true)] // Becomes Global Log Level
158+
[InlineData("", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "*", "Off", true)]
155159
[InlineData("Test", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "Debug", true)]
156160
[InlineData("Test", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "false", true)]
157161
[InlineData("Test", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "no", true)]
@@ -178,6 +182,43 @@ public void EventExclusionPlugin_LogLevels(string source, string level, string s
178182
Assert.Equal(cancelled, context.Cancel);
179183
}
180184

185+
[Fact]
186+
public void EventExclusionPlugin_LogLevels_GetMinLogLevel_Settings_Order() {
187+
var settings = new SettingsDictionary {{"@@log:", "Info"}, {"@@log:*", "Debug"}};
188+
Assert.Equal(LogLevel.Debug, settings.GetMinLogLevel(null));
189+
Assert.Equal(LogLevel.Info, settings.GetMinLogLevel(String.Empty));
190+
Assert.Equal(LogLevel.Debug, settings.GetMinLogLevel("*"));
191+
192+
settings = new SettingsDictionary {{"@@log:*", "Debug"}, {"@@log:", "Info"}};
193+
Assert.Equal(LogLevel.Info, settings.GetMinLogLevel(String.Empty));
194+
Assert.Equal(LogLevel.Debug, settings.GetMinLogLevel("*"));
195+
196+
settings = new SettingsDictionary {
197+
{ "@@log:*", "Fatal" },
198+
{ "@@log:", "Debug" },
199+
{ "@@log:abc*", "Off" },
200+
{ "@@log:abc.de*", "Debug" },
201+
{ "@@log:abc.def*", "Info" },
202+
{ "@@log:abc.def.ghi", "Trace" }
203+
};
204+
205+
Assert.Equal(LogLevel.Fatal, settings.GetMinLogLevel(null));
206+
Assert.Equal(LogLevel.Debug, settings.GetMinLogLevel(String.Empty));
207+
Assert.Equal(LogLevel.Off, settings.GetMinLogLevel("abc"));
208+
Assert.Equal(LogLevel.Info, settings.GetMinLogLevel("abc.def"));
209+
Assert.Equal(LogLevel.Trace, settings.GetMinLogLevel("abc.def.ghi"));
210+
211+
settings = new SettingsDictionary {
212+
{ "@@log:abc.def.ghi", "Trace" },
213+
{ "@@log:abc.def*", "Info" },
214+
{ "@@log:abc*", "Off" }
215+
};
216+
217+
Assert.Equal(LogLevel.Off, settings.GetMinLogLevel("abc"));
218+
Assert.Equal(LogLevel.Info, settings.GetMinLogLevel("abc.def"));
219+
Assert.Equal(LogLevel.Trace, settings.GetMinLogLevel("abc.def.ghi"));
220+
}
221+
181222
[Theory]
182223
[InlineData(null, null, null, null, false)]
183224
[InlineData("usage", null, null, null, false)]
@@ -186,6 +227,9 @@ public void EventExclusionPlugin_LogLevels(string source, string level, string s
186227
[InlineData("usage", "test", "@@usage:Test", "false", true)]
187228
[InlineData("usage", "EX-FEAT: 1234567890", "@@usage:EX-FEAT: 1234567890", "false", true)]
188229
[InlineData("usage", "test", "@@usage:*", "false", true)]
230+
[InlineData("404", null, "@@404:*", "false", true)]
231+
[InlineData("404", null, "@@404:", "false", true)]
232+
[InlineData("404", "", "@@404:", "false", true)]
189233
[InlineData("404", "/unknown", "@@404:*", "false", true)]
190234
[InlineData("404", "/unknown", "@@404:/unknown", "false", true)]
191235
[InlineData("404", "/unknown", "@@404:/unknown", "true", false)]
@@ -200,6 +244,7 @@ public void EventExclusionPlugin_SourceType(string type, string source, string s
200244
var plugin = new EventExclusionPlugin();
201245
plugin.Run(context);
202246
Assert.Equal(cancelled, context.Cancel);
247+
Assert.Equal(source, ev.Source);
203248
}
204249

205250
[Theory]

0 commit comments

Comments
 (0)