9
9
namespace Exceptionless . Plugins . Default {
10
10
[ Priority ( 1010 ) ]
11
11
public class DuplicateCheckerPlugin : IEventPlugin , IDisposable {
12
+ private const string LOG_SOURCE = nameof ( DuplicateCheckerPlugin ) ;
13
+ private static readonly Type _logSourceType = typeof ( DuplicateCheckerPlugin ) ;
12
14
private readonly Queue < Tuple < int , DateTimeOffset > > _processed = new Queue < Tuple < int , DateTimeOffset > > ( ) ;
13
15
private readonly Queue < MergedEvent > _mergedEvents = new Queue < MergedEvent > ( ) ;
14
16
private readonly object _lock = new object ( ) ;
@@ -30,33 +32,36 @@ public DuplicateCheckerPlugin(TimeSpan? interval) {
30
32
}
31
33
32
34
public void Run ( EventPluginContext context ) {
35
+ if ( LOG_SOURCE == context . Event . Source )
36
+ return ;
37
+
33
38
int hashCode = context . Event . GetHashCode ( ) ;
34
39
int count = context . Event . Count ?? 1 ;
35
- context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , "Checking event: {0} with hash: {1}" , context . Event . Message , hashCode ) ;
40
+ context . Log . FormattedTrace ( _logSourceType , "Checking event: {0} with hash: {1}" , context . Event . Message , hashCode ) ;
36
41
37
42
lock ( _lock ) {
38
43
// Increment the occurrence count if the event is already queued for submission.
39
44
var merged = _mergedEvents . FirstOrDefault ( s => s . HashCode == hashCode ) ;
40
45
if ( merged != null ) {
41
46
merged . IncrementCount ( count ) ;
42
47
merged . UpdateDate ( context . Event . Date ) ;
43
- context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , "Ignoring duplicate event with hash: {0}" , hashCode ) ;
48
+ context . Log . FormattedInfo ( _logSourceType , "Ignoring duplicate event: {0} with hash: {1}" , context . Event . Message , hashCode ) ;
44
49
context . Cancel = true ;
45
50
return ;
46
51
}
47
52
48
53
DateTimeOffset repeatWindow = DateTimeOffset . UtcNow . Subtract ( _interval ) ;
49
54
if ( _processed . Any ( s => s . Item1 == hashCode && s . Item2 >= repeatWindow ) ) {
50
- context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , "Adding event with hash: {0 } to cache." , hashCode ) ;
55
+ context . Log . FormattedTrace ( _logSourceType , "Adding duplicate event: {0} with hash: {1 } to cache for later submission." , context . Event . Message , hashCode ) ;
51
56
// This event is a duplicate for the first time, lets save it so we can delay it while keeping count
52
57
_mergedEvents . Enqueue ( new MergedEvent ( hashCode , context , count ) ) ;
53
58
context . Cancel = true ;
54
59
return ;
55
60
}
56
61
57
- context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , "Enqueueing event with hash: {0} to cache." , hashCode ) ;
62
+ context . Log . FormattedTrace ( _logSourceType , "Enqueueing event with hash: {0} to cache." , hashCode ) ;
58
63
_processed . Enqueue ( Tuple . Create ( hashCode , DateTimeOffset . UtcNow ) ) ;
59
-
64
+
60
65
while ( _processed . Count > 50 )
61
66
_processed . Dequeue ( ) ;
62
67
}
@@ -108,15 +113,15 @@ public void Resubmit() {
108
113
_context . Event . Date = DateTimeOffset . Now ;
109
114
110
115
if ( ! _context . Client . OnSubmittingEvent ( _context . Event , _context . ContextData ) ) {
111
- _context . Log . FormattedInfo ( typeof ( DuplicateCheckerPlugin ) , "Event submission cancelled by event handler: id={0} type={1}" , _context . Event . ReferenceId , _context . Event . Type ) ;
116
+ _context . Log . FormattedInfo ( _logSourceType , "Event submission cancelled by event handler: id={0} type={1}" , _context . Event . ReferenceId , _context . Event . Type ) ;
112
117
return ;
113
118
}
114
119
115
- _context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , "Submitting event: type={0}{1}" , _context . Event . Type , ! String . IsNullOrEmpty ( _context . Event . ReferenceId ) ? " refid=" + _context . Event . ReferenceId : String . Empty ) ;
120
+ _context . Log . FormattedTrace ( _logSourceType , "Submitting event: type={0}{1}" , _context . Event . Type , ! String . IsNullOrEmpty ( _context . Event . ReferenceId ) ? " refid=" + _context . Event . ReferenceId : String . Empty ) ;
116
121
_context . Resolver . GetEventQueue ( ) . Enqueue ( _context . Event ) ;
117
122
118
123
if ( ! String . IsNullOrEmpty ( _context . Event . ReferenceId ) ) {
119
- _context . Log . FormattedTrace ( typeof ( DuplicateCheckerPlugin ) , "Setting last reference id: {0}" , _context . Event . ReferenceId ) ;
124
+ _context . Log . FormattedTrace ( _logSourceType , "Setting last reference id: {0}" , _context . Event . ReferenceId ) ;
120
125
_context . Resolver . GetLastReferenceIdManager ( ) . SetLast ( _context . Event . ReferenceId ) ;
121
126
}
122
127
0 commit comments