public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
            {
                var logEntry = data as LogEntry;
                var currentDate = DateTime.Now;
                EmailLogEntry emailLogEntry;
                IList<EmailLogEntry> previousOccurrences = new List<EmailLogEntry>();

                if (logEntry != null)
                {
                    emailLogEntry = new EmailLogEntry(logEntry);
                    switch (logEntry.Severity)
                    {
                        case TraceEventType.Verbose:
                            previousOccurrences = this.GetPreviousOccurrences(this.debugCache, logEntry);
                            this.debugCache.Add(
                                Interlocked.Increment(ref debugCount).ToString(CultureInfo.InvariantCulture),
                                emailLogEntry,
                                cacheItemPolicyFactory.CreatePolicy());
                            break;

                        case TraceEventType.Warning:
                            previousOccurrences = this.GetPreviousOccurrences(this.warningCache, logEntry);
                            this.warningCache.Add(
                                Interlocked.Increment(ref warnCount).ToString(CultureInfo.InvariantCulture),
                                emailLogEntry,
                                cacheItemPolicyFactory.CreatePolicy());
                            break;

                        case TraceEventType.Error:
                        case TraceEventType.Critical:
                            previousOccurrences = this.GetPreviousOccurrences(this.errorCache, logEntry);
                            this.errorCache.Add(
                                Interlocked.Increment(ref errorCount).ToString(CultureInfo.InvariantCulture),
                                emailLogEntry,
                                cacheItemPolicyFactory.CreatePolicy());
                            break;

                        case TraceEventType.Information:
                            previousOccurrences = this.GetPreviousOccurrences(this.infoCache, logEntry);
                            this.infoCache.Add(
                                Interlocked.Increment(ref infoCount).ToString(CultureInfo.InvariantCulture),
                                emailLogEntry,
                                cacheItemPolicyFactory.CreatePolicy());
                            break;
                    }

                    if (previousOccurrences.Any())
                    {
                        if (previousOccurrences.Any(x=> x.HasSendInEmail))
                        {
                            return;
                        }

                        logEntry.ExtendedProperties.Add("Number of occurrences in "+this.emailCacheDuration +" hour(s)", previousOccurrences.Count);
                    }

                    emailLogEntry.HasSendInEmail = true;
                    emailLogEntry.EmailSendOn = currentDate;
                }

                base.TraceData(eventCache, source, eventType, id, data);
            }
        }
Beispiel #2
0
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if ((this.Filter == null) || this.Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
            {
                var                   logEntry    = data as LogEntry;
                var                   currentDate = DateTime.Now;
                EmailLogEntry         emailLogEntry;
                IList <EmailLogEntry> previousOccurrences = new List <EmailLogEntry>();

                if (logEntry != null)
                {
                    emailLogEntry = new EmailLogEntry(logEntry);
                    switch (logEntry.Severity)
                    {
                    case TraceEventType.Verbose:
                        previousOccurrences = this.GetPreviousOccurrences(this.debugCache, logEntry);
                        this.debugCache.Add(
                            Interlocked.Increment(ref debugCount).ToString(CultureInfo.InvariantCulture),
                            emailLogEntry,
                            cacheItemPolicyFactory.CreatePolicy());
                        break;

                    case TraceEventType.Warning:
                        previousOccurrences = this.GetPreviousOccurrences(this.warningCache, logEntry);
                        this.warningCache.Add(
                            Interlocked.Increment(ref warnCount).ToString(CultureInfo.InvariantCulture),
                            emailLogEntry,
                            cacheItemPolicyFactory.CreatePolicy());
                        break;

                    case TraceEventType.Error:
                    case TraceEventType.Critical:
                        previousOccurrences = this.GetPreviousOccurrences(this.errorCache, logEntry);
                        this.errorCache.Add(
                            Interlocked.Increment(ref errorCount).ToString(CultureInfo.InvariantCulture),
                            emailLogEntry,
                            cacheItemPolicyFactory.CreatePolicy());
                        break;

                    case TraceEventType.Information:
                        previousOccurrences = this.GetPreviousOccurrences(this.infoCache, logEntry);
                        this.infoCache.Add(
                            Interlocked.Increment(ref infoCount).ToString(CultureInfo.InvariantCulture),
                            emailLogEntry,
                            cacheItemPolicyFactory.CreatePolicy());
                        break;
                    }

                    if (previousOccurrences.Any())
                    {
                        if (previousOccurrences.Any(x => x.HasSendInEmail))
                        {
                            return;
                        }

                        logEntry.ExtendedProperties.Add("Number of occurrences in " + this.emailCacheDuration + " hour(s)", previousOccurrences.Count);
                    }

                    emailLogEntry.HasSendInEmail = true;
                    emailLogEntry.EmailSendOn    = currentDate;
                }

                base.TraceData(eventCache, source, eventType, id, data);
            }
        }