Skip to content

Commit 73fa9de

Browse files
committed
Added unit tests for EventExclusions
1 parent ea53c37 commit 73fa9de

File tree

5 files changed

+141
-31
lines changed

5 files changed

+141
-31
lines changed

Source/Shared/Extensions/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ public static bool IsPatternMatch(this string value, string pattern, bool ignore
4747
if (endsWithWildcard)
4848
pattern = pattern.Substring(0, pattern.Length - 1);
4949

50-
if (ignoreCase)
50+
if (ignoreCase) {
51+
value = value.ToLower();
5152
pattern = pattern.ToLower();
53+
}
5254

5355
if (startsWithWildcard && endsWithWildcard)
5456
return value.Contains(pattern);

Source/Shared/Models/Collections/SettingsDictionary.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public string GetString(string name) {
2121
public string GetString(string name, string @default) {
2222
string value;
2323

24-
if (TryGetValue(name, out value))
24+
if (name != null && TryGetValue(name, out value))
2525
return value;
2626

2727
return @default;
@@ -33,9 +33,9 @@ public bool GetBoolean(string name) {
3333

3434
public bool GetBoolean(string name, bool @default) {
3535
bool value;
36-
string temp;
36+
string temp = null;
3737

38-
bool result = TryGetValue(name, out temp);
38+
bool result = name != null && TryGetValue(name, out temp);
3939
if (!result)
4040
return @default;
4141

@@ -49,9 +49,9 @@ public int GetInt32(string name) {
4949

5050
public int GetInt32(string name, int @default) {
5151
int value;
52-
string temp;
52+
string temp = null;
5353

54-
bool result = TryGetValue(name, out temp);
54+
bool result = name != null && TryGetValue(name, out temp);
5555
if (!result)
5656
return @default;
5757

@@ -65,9 +65,9 @@ public long GetInt64(string name) {
6565

6666
public long GetInt64(string name, long @default) {
6767
long value;
68-
string temp;
68+
string temp = null;
6969

70-
bool result = TryGetValue(name, out temp);
70+
bool result = name != null && TryGetValue(name, out temp);
7171
if (!result)
7272
return @default;
7373

@@ -77,9 +77,9 @@ public long GetInt64(string name, long @default) {
7777

7878
public double GetDouble(string name, double @default = 0d) {
7979
double value;
80-
string temp;
80+
string temp = null;
8181

82-
bool result = TryGetValue(name, out temp);
82+
bool result = name != null && TryGetValue(name, out temp);
8383
if (!result)
8484
return @default;
8585

@@ -93,9 +93,9 @@ public DateTime GetDateTime(string name) {
9393

9494
public DateTime GetDateTime(string name, DateTime @default) {
9595
DateTime value;
96-
string temp;
96+
string temp = null;
9797

98-
bool result = TryGetValue(name, out temp);
98+
bool result = name != null && TryGetValue(name, out temp);
9999
if (!result)
100100
return @default;
101101

@@ -109,9 +109,9 @@ public DateTimeOffset GetDateTimeOffset(string name) {
109109

110110
public DateTimeOffset GetDateTimeOffset(string name, DateTimeOffset @default) {
111111
DateTimeOffset value;
112-
string temp;
112+
string temp = null;
113113

114-
bool result = TryGetValue(name, out temp);
114+
bool result = name != null && TryGetValue(name, out temp);
115115
if (!result)
116116
return @default;
117117

@@ -124,9 +124,9 @@ public Guid GetGuid(string name) {
124124
}
125125

126126
public Guid GetGuid(string name, Guid @default) {
127-
string temp;
127+
string temp = null;
128128

129-
bool result = TryGetValue(name, out temp);
129+
bool result = name != null && TryGetValue(name, out temp);
130130
return result ? new Guid(temp) : @default;
131131
}
132132

@@ -168,7 +168,7 @@ protected override void OnChanged(ChangedEventArgs<KeyValuePair<string, string>>
168168

169169
foreach (var eventType in _eventTypes) {
170170
Dictionary<string, bool> sourceDictionary;
171-
if (!_typeSourceEnabled.TryGetValue(eventType.Key, out sourceDictionary))
171+
if (eventType.Key == null || !_typeSourceEnabled.TryGetValue(eventType.Key, out sourceDictionary))
172172
continue;
173173

174174
if (!args.Item.Key.StartsWith(eventType.Value))
@@ -189,6 +189,9 @@ protected override void OnChanged(ChangedEventArgs<KeyValuePair<string, string>>
189189

190190
private readonly Dictionary<string, LogLevel> _minLogLevels = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase);
191191
public LogLevel GetMinLogLevel(string loggerName) {
192+
if (String.IsNullOrEmpty(loggerName))
193+
loggerName = "*";
194+
192195
LogLevel minLogLevel;
193196
if (_minLogLevels.TryGetValue(loggerName, out minLogLevel))
194197
return minLogLevel;
@@ -206,9 +209,12 @@ public LogLevel GetMinLogLevel(string loggerName) {
206209

207210
private readonly Dictionary<string, Dictionary<string, bool>> _typeSourceEnabled = new Dictionary<string, Dictionary<string, bool>>(StringComparer.OrdinalIgnoreCase);
208211
public bool GetTypeAndSourceEnabled(string type, string source) {
212+
if (type == null)
213+
return true;
214+
209215
Dictionary<string, bool> sourceDictionary;
210216
bool sourceEnabled;
211-
if (_typeSourceEnabled.TryGetValue(type, out sourceDictionary)) {
217+
if (source != null && _typeSourceEnabled.TryGetValue(type, out sourceDictionary)) {
212218
if (sourceDictionary.TryGetValue(source, out sourceEnabled))
213219
return sourceEnabled;
214220
}
@@ -222,6 +228,9 @@ public bool GetTypeAndSourceEnabled(string type, string source) {
222228

223229
private readonly Dictionary<string, string> _eventTypes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
224230
private string GetTypeAndSourceSetting(string type, string source, string defaultValue) {
231+
if (type == null)
232+
return defaultValue;
233+
225234
Dictionary<string, bool> sourceDictionary;
226235
string sourcePrefix;
227236
if (!_typeSourceEnabled.TryGetValue(type, out sourceDictionary)) {
@@ -237,7 +246,7 @@ private string GetTypeAndSourceSetting(string type, string source, string defaul
237246
string settingValue;
238247
if (TryGetValue(sourcePrefix + source, out settingValue))
239248
return settingValue;
240-
249+
241250
// check for wildcard match
242251
var sourceSettings = this
243252
.Where(kvp => kvp.Key.StartsWith(sourcePrefix))

Source/Shared/Plugins/Default/005_HandleAggregateExceptionsPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public void Run(EventPluginContext context) {
2727
context.Cancel = true;
2828
}
2929
}
30-
}
30+
}

Source/Shared/Plugins/Default/010_EventExclusionPlugin.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Exceptionless.Plugins.Default {
66
[Priority(10)]
7-
public class EventExclusionPlugin : IEventPlugin {
7+
public sealed class EventExclusionPlugin : IEventPlugin {
88
public void Run(EventPluginContext context) {
99
foreach (var callback in context.Client.Configuration.EventExclusions) {
1010
try {
@@ -35,16 +35,20 @@ public void Run(EventPluginContext context) {
3535
context.Cancel = true;
3636
return;
3737
}
38+
39+
try {
40+
var exception = context.ContextData.GetException();
41+
while (exception != null) {
42+
if (!context.Client.Configuration.Settings.GetTypeAndSourceEnabled(Event.KnownTypes.Error, exception.GetType().FullName)) {
43+
context.Log.Info("Cancelling error from excluded exception type: " + exception.GetType().FullName);
44+
context.Cancel = true;
45+
return;
46+
}
3847

39-
var exception = context.ContextData.GetException();
40-
while (exception != null) {
41-
if (!context.Client.Configuration.Settings.GetTypeAndSourceEnabled(Event.KnownTypes.Error, exception.GetType().FullName)) {
42-
context.Log.Info("Cancelling error from excluded exception type: " + exception.GetType().FullName);
43-
context.Cancel = true;
44-
return;
48+
exception = exception.InnerException;
4549
}
46-
47-
exception = exception.InnerException;
50+
} catch (Exception ex) {
51+
context.Log.Error(ex, "Error checking excluded exception types: " + ex.Message);
4852
}
4953
}
5054
}

Source/Tests/Plugins/PluginTests.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,95 @@ public void ConfigurationDefaults_SerializedProperties() {
120120
Assert.Equal("blake", context.Event.GetUserIdentity().Identity);
121121
Assert.Equal("blake", context.Event.GetUserIdentity(serializer).Identity);
122122
}
123+
124+
[Fact]
125+
public void EventExclusionPlugin_EventExclusions() {
126+
var client = CreateClient();
127+
var plugin = new EventExclusionPlugin();
128+
129+
// ignore any event that has a value of 2
130+
client.Configuration.AddEventExclusion(e => e.Value.GetValueOrDefault() != 2);
131+
132+
var ev = new Event { Value = 1 };
133+
var context = new EventPluginContext(client, ev);
134+
plugin.Run(context);
135+
Assert.False(context.Cancel);
136+
137+
ev.Value = 2;
138+
context = new EventPluginContext(client, ev);
139+
plugin.Run(context);
140+
Assert.True(context.Cancel);
141+
}
142+
143+
[Theory]
144+
[InlineData(null, null, null, null, false)]
145+
[InlineData("Test", null, null, null, false)]
146+
[InlineData("Test", "Trace", null, null, false)]
147+
[InlineData("Test", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "Debug", true)]
148+
[InlineData("Test", "Info", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "Debug", false)]
149+
[InlineData("Test", "Trace", SettingsDictionary.KnownKeys.LogLevelPrefix + "*", "Debug", true)]
150+
[InlineData("Test", "Warn", SettingsDictionary.KnownKeys.LogLevelPrefix + "*", "Debug", false)]
151+
public void EventExclusionPlugin_LogLevels(string source, string level, string settingKey, string settingValue, bool cancelled) {
152+
var client = CreateClient();
153+
if (settingKey != null)
154+
client.Configuration.Settings.Add(settingKey, settingValue);
155+
156+
var ev = new Event { Type = Event.KnownTypes.Log, Source = source };
157+
if (!String.IsNullOrEmpty(level))
158+
ev.SetProperty(Event.KnownDataKeys.Level, level);
159+
160+
var context = new EventPluginContext(client, ev);
161+
var plugin = new EventExclusionPlugin();
162+
plugin.Run(context);
163+
Assert.Equal(cancelled, context.Cancel);
164+
}
123165

166+
[Theory]
167+
[InlineData(null, null, null, null, true)]
168+
[InlineData("Test", null, null, null, true)]
169+
[InlineData("Test", "Trace", null, null, true)]
170+
[InlineData("Test", "Warn", null, null, false)]
171+
[InlineData("Test", "Error", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "Debug", false)]
172+
[InlineData("Test", "Info", SettingsDictionary.KnownKeys.LogLevelPrefix + "Test", "Debug", false)]
173+
public void EventExclusionPlugin_LogLevelsWithInfoDefault(string source, string level, string settingKey, string settingValue, bool cancelled) {
174+
var client = CreateClient();
175+
client.Configuration.Settings.Add(SettingsDictionary.KnownKeys.LogLevelPrefix + "*", "Info");
176+
if (settingKey != null)
177+
client.Configuration.Settings.Add(settingKey, settingValue);
178+
179+
var ev = new Event { Type = Event.KnownTypes.Log, Source = source };
180+
if (!String.IsNullOrEmpty(level))
181+
ev.SetProperty(Event.KnownDataKeys.Level, level);
182+
183+
var context = new EventPluginContext(client, ev);
184+
var plugin = new EventExclusionPlugin();
185+
plugin.Run(context);
186+
Assert.Equal(cancelled, context.Cancel);
187+
}
188+
189+
[Theory]
190+
[InlineData(null, false)]
191+
[InlineData("@@error:TestException", false)]
192+
[InlineData("@@error:Exception", false)]
193+
[InlineData("@@error:System.Exception", true)]
194+
[InlineData("@@error:*Exception", true)]
195+
[InlineData("@@error:*", true)]
196+
public void EventExclusionPlugin_SimpleException(string settingKey, bool cancelled) {
197+
var client = CreateClient();
198+
if (settingKey != null)
199+
client.Configuration.Settings.Add(settingKey, Boolean.FalseString);
200+
201+
var plugin = new EventExclusionPlugin();
202+
var context = new EventPluginContext(client, new Event());
203+
context.ContextData.SetException(GetException());
204+
plugin.Run(context);
205+
Assert.Equal(cancelled, context.Cancel);
206+
207+
context.ContextData.SetException(GetNestedSimpleException());
208+
plugin.Run(context);
209+
Assert.Equal(cancelled, context.Cancel);
210+
}
211+
124212
[Fact]
125213
public void IgnoreUserAgentPlugin_DiscardBot() {
126214
var client = CreateClient();
@@ -592,8 +680,7 @@ public void VerifyDeduplicationMultithreaded() {
592680
}
593681

594682
[Fact]
595-
public void VerifyDeduplicationFromFiles()
596-
{
683+
public void VerifyDeduplicationFromFiles() {
597684
var client = CreateClient();
598685
var errorPlugin = new ErrorPlugin();
599686

@@ -628,6 +715,14 @@ private Exception GetException(string message = "Test") {
628715
}
629716
}
630717

718+
private Exception GetNestedSimpleException(string message = "Test") {
719+
try {
720+
throw new Exception("nested " + message);
721+
} catch (Exception ex) {
722+
return new ApplicationException(message, ex);
723+
}
724+
}
725+
631726
[Fact]
632727
public void RunBenchmark() {
633728
var summary = BenchmarkRunner.Run<DeduplicationBenchmarks>();

0 commit comments

Comments
 (0)