public override async Task EventProcessingAsync(EventContext context)
        {
            if (String.IsNullOrEmpty(context.Event.ReferenceId))
            {
                return;
            }

            if (await _cacheClient.AddAsync(GetCacheKey(context), true, TimeSpan.FromDays(1)).AnyContext())
            {
                context.SetProperty("AddedReferenceId", true);
                return;
            }

            _logger.Warn().Project(context.Event.ProjectId).Message("Discarding event due to duplicate reference id: {0}", context.Event.ReferenceId).Write();
            context.IsCancelled = true;
        }
        public override void Process(EventContext ctx) {
            if (String.IsNullOrEmpty(ctx.Event.StackId)) {
                if (_stackRepository == null)
                    throw new InvalidOperationException("You must pass a non-null stackRepository parameter to the constructor.");

                // only add default signature info if no other signature info has been added
                if (ctx.StackSignatureData.Count == 0) {
                    ctx.StackSignatureData.Add("Type", ctx.Event.Type);
                    if (!String.IsNullOrEmpty(ctx.Event.Source))
                        ctx.StackSignatureData.Add("Source", ctx.Event.Source);
                }

                string signatureHash = ctx.StackSignatureData.Values.Any(v => v != null) ? ctx.StackSignatureData.Values.ToSHA1() : null;
                ctx.SetProperty("__SignatureHash", signatureHash);

                ctx.StackInfo = _stackRepository.GetStackInfoBySignatureHash(ctx.Event.ProjectId, signatureHash);
                if (ctx.StackInfo == null) {
                    Log.Trace().Message("Creating new error stack.").Write();
                    ctx.IsNew = true;
                    var stack = new Stack {
                        OrganizationId = ctx.Event.OrganizationId,
                        ProjectId = ctx.Event.ProjectId,
                        SignatureInfo = new SettingsDictionary(ctx.StackSignatureData),
                        SignatureHash = signatureHash,
                        Title = _pluginManager.GetStackTitle(ctx.Event),
                        SummaryHtml = _pluginManager.GetStackSummaryHtml(ctx.Event),
                        Tags = ctx.Event.Tags ?? new TagSet(),
                        TotalOccurrences = 1,
                        FirstOccurrence = ctx.Event.Date.UtcDateTime,
                        LastOccurrence = ctx.Event.Date.UtcDateTime
                    };

                    ctx.Stack = _stackRepository.Add(stack, true);
                    ctx.StackInfo = new StackInfo {
                        Id = stack.Id,
                        DateFixed = stack.DateFixed,
                        OccurrencesAreCritical = stack.OccurrencesAreCritical
                    };

                    // new 404 stack id added, invalidate 404 id cache
                    if (ctx.Event.IsNotFound())
                        _stackRepository.InvalidateNotFoundIdsCache(ctx.Event.ProjectId);
                }

                Log.Trace().Message("Updating error's ErrorStackId to: {0}", ctx.StackInfo.Id).Write();
                ctx.Event.StackId = ctx.StackInfo.Id;
            } else {
                ctx.Stack = _stackRepository.GetById(ctx.Event.StackId);

                // TODO: Update unit tests to work with this check.
                //if (stack == null || stack.ProjectId != error.ProjectId)
                //    throw new InvalidOperationException("Invalid ErrorStackId.");
                if (ctx.Stack == null)
                    return;

                ctx.SetProperty("__SignatureHash", ctx.Stack.SignatureHash);

                if (ctx.Event.Tags != null && ctx.Event.Tags.Count > 0) {
                    if (ctx.Stack.Tags == null)
                        ctx.Stack.Tags = new TagSet();

                    List<string> newTags = ctx.Event.Tags.Where(t => !ctx.Stack.Tags.Contains(t)).ToList();
                    if (newTags.Count > 0) {
                        ctx.Stack.Tags.AddRange(newTags);
                        _stackRepository.Save(ctx.Stack);
                    }
                }

                ctx.StackInfo = new StackInfo {
                    Id = ctx.Stack.Id,
                    DateFixed = ctx.Stack.DateFixed,
                    OccurrencesAreCritical = ctx.Stack.OccurrencesAreCritical,
                    IsHidden = ctx.Stack.IsHidden,
                };
            }

            // sync the fixed and hidden flags to the error occurrence
            ctx.Event.IsFixed = ctx.StackInfo.DateFixed.HasValue;
            ctx.Event.IsHidden = ctx.StackInfo.IsHidden;

            ctx.Event.SummaryHtml = _pluginManager.GetEventSummaryHtml(ctx.Event);
        }
        public override void Process(EventContext ctx) {
            if (String.IsNullOrEmpty(ctx.Event.StackId)) {
                if (_stackRepository == null)
                    throw new InvalidOperationException("You must pass a non-null stackRepository parameter to the constructor.");

                // only add default signature info if no other signature info has been added
                if (ctx.StackSignatureData.Count == 0) {
                    ctx.StackSignatureData.Add("Type", ctx.Event.Type);
                    if (!String.IsNullOrEmpty(ctx.Event.Source))
                        ctx.StackSignatureData.Add("Source", ctx.Event.Source);
                }

                string signatureHash = ctx.StackSignatureData.Values.Any(v => v != null) ? ctx.StackSignatureData.Values.ToSHA1() : null;
                ctx.SetProperty("__SignatureHash", signatureHash);

                ctx.Stack = _stackRepository.GetStackBySignatureHash(ctx.Event.ProjectId, signatureHash);
                if (ctx.Stack == null) {
                    Log.Trace().Message("Creating new error stack.").Write();
                    ctx.IsNew = true;
                    ctx.Event.IsFirstOccurrence = true;

                    string title = _formattingPluginManager.GetStackTitle(ctx.Event);
                    var stack = new Stack {
                        OrganizationId = ctx.Event.OrganizationId,
                        ProjectId = ctx.Event.ProjectId,
                        SignatureInfo = new SettingsDictionary(ctx.StackSignatureData),
                        SignatureHash = signatureHash,
                        Title = title != null ? title.Truncate(1000) : null,
                        Tags = ctx.Event.Tags ?? new TagSet(),
                        Type = ctx.Event.Type,
                        TotalOccurrences = 1,
                        FirstOccurrence = ctx.Event.Date.UtcDateTime,
                        LastOccurrence = ctx.Event.Date.UtcDateTime
                    };

                    ctx.Stack = _stackRepository.Add(stack, true);
                }

                Log.Trace().Message("Updating error's StackId to: {0}", ctx.Stack.Id).Write();
                ctx.Event.StackId = ctx.Stack.Id;
            } else {
                ctx.Stack = _stackRepository.GetById(ctx.Event.StackId, true);

                if (ctx.Stack == null || ctx.Stack.ProjectId != ctx.Event.ProjectId)
                    throw new ApplicationException("Invalid StackId.");

                ctx.SetProperty("__SignatureHash", ctx.Stack.SignatureHash);
            }

            if (!ctx.IsNew && ctx.Event.Tags != null && ctx.Event.Tags.Count > 0) {
                if (ctx.Stack.Tags == null)
                    ctx.Stack.Tags = new TagSet();

                List<string> newTags = ctx.Event.Tags.Where(t => !ctx.Stack.Tags.Contains(t)).ToList();
                if (newTags.Count > 0) {
                    ctx.Stack.Tags.AddRange(newTags);
                    _stackRepository.Save(ctx.Stack, true);
                }
            }

            // sync the fixed and hidden flags to the error occurrence
            ctx.Event.IsFixed = ctx.Stack.DateFixed.HasValue;
            ctx.Event.IsHidden = ctx.Stack.IsHidden;
        }